Javanes
Javanes

Reputation: 91

How to insert Bitmap into HashMap

Well, I've been trying to insert images from the the Web into a Listview. When I use images from drawable file, inserting "R.drawable.paris (foto file name)", everything goes ok. But when I try to use a Bitmap object, the image just do not appear on the screen. Anybody could tell me what I'm doing wrong?

public class TesHashBitActivity extends Activity {
    /** Called when the activity is first created. */

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);


        ListView listCities = (ListView)findViewById(R.id.citylist);

        Bitmap photo = BitmapFactory.decodeResource(getResources(), R.drawable.img1);
        ImageView img = (ImageView)findViewById(R.id.imagedisplay);
        img.setImageBitmap(photo);

        ArrayList<HashMap<String, Object>> citiesmap  =  new ArrayList<HashMap<String, Object>>();

        HashMap<String, Object> citmap = new HashMap<String, Object>();

        citmap.put("Photo", photo);
        citmap.put("Nation", "France");
        citiesmap.add(citmap);


        SimpleAdapter sadapt = new SimpleAdapter(this, citiesmap, R.layout.lines, new String[] {"Photo","Nation"}, new int[] {R.id.photoimg,R.id.contentline});

        listCities.setAdapter(sadapt);
        }
}

Upvotes: 0

Views: 4208

Answers (1)

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28379

You are not understanding ListView correctly...

Take a look at this tutorial...

http://www.vogella.de/articles/AndroidListView/article.html

The thing you're clearly missing is how you define the View that the list view uses as its template (for an individual element to be displayed). You can't just toss it a list of images and have it figure stuff out. You need to provide it with a View template that it will repurpose over and over, and then give it the appropriate values to populate into those Views.

In the example above, look for R.layout.rowlayout. That's where he explains how to use a custom View as your list element.

And welcome to Stack! :) Don't forget to mark correct answers as such, and upvote the ones that you find most helpful.

Upvotes: 1

Related Questions