Set image url into listadapter to show images using Picasso

百般思念 提交于 2019-12-25 18:37:33

问题


I have a simple list adapter in which I want to display food images with some text info. Till now I have known that I can do it with libraries like AQuery or Picasso, but I can't figure it out how to implement it after reading a lot of tutorials.

I have added Picasso library in my dependencies. Suppose my Image view id is ImgViewFood

activity

JSONArray jsonArr = jsonObj.getJSONArray("foodnames");
                for (int i = 0; i < jsonArr.length(); i++) {
                    HashMap<String, String> food = new HashMap<>();
                    JSONObject jsonObj = jsonArr.getJSONObject(i);

                    String name = jsonObj.getString("food_name");
                    String url = jsonObj.getString("img_url");

                    food.put("foodname", name);
                    foodList.add(food);
                }
                ListAdapter adapter = new SimpleAdapter(
                        FoodListActivity.this, foodList,
                        R.layout.list_food_detail, new String[]{"foodname"}, new int[]{R.id.view_food_name});

                listviewFood.setAdapter(adapter);

回答1:


Complete Solution

List adapter

JSONArray jsonArr = jsonObj.getJSONArray("foodnames");
                for (int i = 0; i < jsonArr.length(); i++) {
                    HashMap<String, String> food = new HashMap<>();
                    JSONObject jsonObj1 = jsonArr.getJSONObject(i);
                    String url = "www.nomadicbong.com/images/";
                    food.put("foodname", jsonObj1.getString("foodname"));
                    food.put("foodid", jsonObj1.getString("foodid"));
                    food.put("imgname",url.concat(jsonObj1.getString("imagename")));
                    foodList.add(food);
                }

               ListAdapter adapter = new CustomImageAdapter(

                        FoodListActivity.this, foodList,
                        R.layout.list_food_detail, new String[]{"foodid", "foodname"}, new int[]{R.id.view_food_id,R.id.view_food_name});

                listviewFood.setAdapter(adapter);

CustomImageAdapter

import com.squareup.picasso.*;

/**
 * Created by nomadicbong on 4/25/2019.
 */

public class CustomImageAdapter extends SimpleAdapter{

    public CustomImageAdapter(Context context, List<? extends Map<String, ?>> foodList, int resource, String[] from, int[] to){
        super(context, foodList, resource, from, to);
    }
    public View getView(int position, View convertView, ViewGroup parent){

        View v = super.getView(position, convertView, parent);

        ImageView imgvw = (ImageView) v.getTag();
        if(imgvw == null){
            imgvw = (ImageView) v.findViewById(R.id.imgFood1);
            v.setTag(imgvw);
        }
        // get the url from the data in the `Map`
        String url = ((Map)getItem(position)).get("imgname").toString();
        Picasso.with(v.getContext()).load(url).into(imgvw);
        return v;
    }
}


来源:https://stackoverflow.com/questions/55860377/set-image-url-into-listadapter-to-show-images-using-picasso

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!