简体   繁体   English

使用他们的URL显示多个图像到带有JSON的listview

[英]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. 因为您每次都在执行findViewById()并设置图像位图。 And then you are adding in mylist. 然后你添加了mylist。

Suggestion: Instead i would suggest you to add only URL string into HashMap: 建议:相反,我建议你只在HashMap中添加URL字符串:

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 ). 在为ListView定义自定义适配器时,只需按照自定义适配器的getView()方法(可以通过扩展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 建议2:如果你想在ListView中实现延迟加载图像,那么请查看Fedor在这里给出的答案: Android - 如何在ListView中执行延迟加载的图像

I hope, ur actual HashMasp is HashMap map = new HashMap (); 我希望,你实际的HashMasp是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. 在你的listview适配器中使用此列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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