簡體   English   中英

使用他們的URL顯示多個圖像到帶有JSON的listview

[英]Displaying multiple images using their URLs to listview with JSON

我一直在嘗試將我的圖片網址加載到第一個必須下載的列表視圖中,有人可以幫助我。

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

你正在用這段代碼做什么?

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

因為您每次都在執行findViewById()並設置圖像位圖。 然后你添加了mylist。

建議:相反,我建議你只在HashMap中添加URL字符串:

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

在為ListView定義自定義適配器時,只需按照自定義適配器的getView()方法(可以通過擴展BaseAdapter定義)進行操作。

建議2:如果你想在ListView中實現延遲加載圖像,那么請查看Fedor在這里給出的答案: Android - 如何在ListView中執行延遲加載的圖像

我希望,你實際的HashMasp是HashMap map = new HashMap();

如果是這樣,你只能添加字符串值。 試試以下,

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

在你的listview適配器中使用此列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM