Skip to content Skip to sidebar Skip to footer

Set Image Url Into Listadapter To Show Images Using Picasso

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 f

Solution 1:

Complete Solution

List adapter

JSONArray jsonArr = jsonObj.getJSONArray("foodnames");
                for (int i = 0; i < jsonArr.length(); i++) {
                    HashMap<String, String> food = newHashMap<>();
                    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 = newCustomImageAdapter(

                        FoodListActivity.this, foodList,
                        R.layout.list_food_detail, newString[]{"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.
 */publicclassCustomImageAdapterextendsSimpleAdapter{

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

Post a Comment for "Set Image Url Into Listadapter To Show Images Using Picasso"