在android自定义适配器中将动态图像设置为ListView



我正在使用web服务器中的项目构建一个列表视图。我用JSON获得了所有项目,我可以将所有文本值加载到列表视图,但我不能将图像分配给列表项目。当我从drawable文件夹访问静态图像时,它就工作了。这是我的密码。我有Base64和直接文件URL的图像。

int[] listviewImage = new int[]{R.drawable.food};
JSONObject reader = new JSONObject(result);
JSONArray foods= reader.getJSONArray("foods");
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for(int i = 0;i<foods.length();i++)
{
JSONObject obj_foods = foods.getJSONObject(i);
HashMap<String, String> hm = new HashMap<String, String>();
byte[] decodedString = Base64.decode(obj_foods .getString("photo_hex"), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
hm.put("listview_image", Integer.toString(listviewImage[0]));
hm.put("food_name", obj_foods .getString("food_name"));
hm.put("food_description",obj_foods .getString("food_description"));
hm.put("price",obj_foods .getString("price"));
hm.put("restaurant",obj_foods .getString("restaurant"));
aList.add(hm);
}
String[] from = {"listview_image", "food_name", "food_description","price","restaurant"};
int[] to = {R.id.listview_image, R.id.listview_item_title, R.id.listview_item_short_description,R.id.price,R.id.restaurant};
SimpleAdapter simpleAdapter = new SimpleAdapter(getBaseContext(), aList, R.layout.food_list, from, to);
final ListView androidListView = (ListView) findViewById(R.id.listview);
androidListView.setAdapter(simpleAdapter);

使用Picasso将在线图像提取到您的应用

implementation 'com.squareup.picasso:picasso:2.71828'
Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);

最新更新