user1207576
user1207576

Reputation: 17

Displaying multiple images using their URLs to listview with JSON

I have been trying to load my image urls onto a listview that is the first have to be downloaded can someone please help me.

HashMap<String, String> map = new HashMap<String, String>();
 map.put("id", String.valueOf(i));
 map.put("name", "House name:" + json_data.getString("name"));
map.put("address", "Adress: " + json_data.getString("address"));

URL newurl = new URL(json_data.getString("imageUrl"));
 itmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(result).getContent());
 ImageView imagy = (ImageView)findViewById(R.id.image);
 imagy.setImageBitmap(bitmap); 
  map.put("img",bitmap);//error is here says convert bitmap to type string
  mylist.add(map);

Upvotes: 0

Views: 4125

Answers (2)

Jayabal
Jayabal

Reputation: 3619

I hope, ur actual HashMasp is HashMap map = new HashMap ();

if so, u can only add String values. try the following,

class House {
    int id;
    String houseName;
    String houseAddress;
    Bitmap image;
}

List<House> houseList = new ArrayList<House> ();

House houseObj = new House();

houseObj.id = i;
houseObj.houseName = "House name:" + json_data.getString("name");
houseObj.address = "Adress: " + json_data.getString("address");

URL newurl = new URL(json_data.getString("imageUrl"));
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(result).getContent());
ImageView imagy = (ImageView)findViewById(R.id.image);
imagy.setImageBitmap(bitmap); 

houseObj.image = bitmap;

houseList.add(houseObj);

use this list in ur listview adapter.

Upvotes: 1

Paresh Mayani
Paresh Mayani

Reputation: 128428

What are you doing exactly with this code?

 URL newurl = new URL(json_data.getString("imageUrl"));
            Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(result).getContent());
            ImageView imagy = (ImageView)findViewById(R.id.image);
              imagy.setImageBitmap(bitmap); 
             map.put("img",bitmap);//error is here says convert bitmap to type string

            mylist.add(map);

Because you are doing findViewById() and setting image bitmap every time. And then you are adding in mylist.

Suggestion: Instead i would suggest you to add only URL string into HashMap:

String strImageURL = json_data.getString("imageUrl");
map.put("img",strImageURL );/

And while defining Custom adapter for your ListView, just do as you are doing above inside the getView() method of your custom adapter (which you can define by extending BaseAdapter).

Suggestion 2: If you want to implement Lazy loading of images inside ListView, then check Fedor's answer given here: Android - How do I do a lazy load of images in ListView

Upvotes: 1

Related Questions