将图片网址设置为listadapter,以使用毕加索显示图片 [英] Set image url into listadapter to show images using Picasso

查看:129
本文介绍了将图片网址设置为listadapter,以使用毕加索显示图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的列表适配器,我想在其中显示带有一些文本信息的食物图像.到现在为止,我已经知道可以使用AQuery或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 figure it out how to implement it after reading a lot of tutorials.

我在依赖项中添加了Picasso库.假设我的图片视图ID为 ImgViewFood

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

活动

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

推荐答案

完整解决方案

Complete Solution

列表适配器

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

这篇关于将图片网址设置为listadapter,以使用毕加索显示图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆