简体   繁体   中英

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);

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM