android listview从url加载图像到imageview

时间:2018-11-22 07:19:16

标签: java android json listview

我想在列表视图中显示图片

    private void ReadDataFromDB() { //this load in to OnCreate
    String user_id = userInfo.getKeyUserId();// sending user_id to the server
    Map<String, String> params = new HashMap<String, String>();
    params.put("user_id", user_id); //putting parameters
          CustomRequest  jreq = new CustomRequest(Method.POST, Utils.HISTORY, params,
            new Response.Listener<JSONObject>() { // doing some custom request
                @Override
                public void onResponse(JSONObject response) {
                    try {

                        int success = response.getInt("success");

                        if (success == 1) {//retriving in json format
                            JSONArray ja = response.getJSONArray("payments");
                            for (int i = 0; i < ja.length(); i++) {

                                JSONObject jobj = ja.getJSONObject(i);
                                HashMap<String, String> item = new 
                                HashMap<String, String>();                                
                                item.put(STATUS, jobj.getString(STATUS));
                                item.put(LOGO, jobj.getString(LOGO));//here i have link to the image
                                Item_List.add(item); //adding to the listview
                            } // for loop ends

                            String[] from = {STATUS,LOGO};
                            int[] to = {R.id.status, R.id.thumbnail};

                            adapter = new SimpleAdapter(
                                    getApplicationContext(), Item_List,
                                    R.layout.list_items, from, to);
                             listview.setAdapter(adapter);
    AndroidLoginController.getInstance().addToRequestQueue(jreq);

}

}

如何为清单视图添加图像并显示它?任何对此的反馈将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

这是您可以根据需要修改的代码

static images class 

    public static String[] eatFoodyImages = {  
        "http://i.imgur.com/rFLNqWI.jpg",
        "http://i.imgur.com/C9pBVt7.jpg",
        "http://i.imgur.com/rT5vXE1.jpg",
        "http://i.imgur.com/aIy5R2k.jpg",
        "http://i.imgur.com/MoJs9pT.jpg",
        "http://i.imgur.com/S963yEM.jpg",
        "http://i.imgur.com/rLR2cyc.jpg",
        "http://i.imgur.com/SEPdUIx.jpg",
        "http://i.imgur.com/aC9OjaM.jpg",
        "http://i.imgur.com/76Jfv9b.jpg",
        "http://i.imgur.com/fUX7EIB.jpg",
        "http://i.imgur.com/syELajx.jpg",
        "http://i.imgur.com/COzBnru.jpg",
        "http://i.imgur.com/Z3QjilA.jpg",
};

UsageExample活动类

    public class UsageExampleActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_usage_example_adapter);

        listView.setAdapter(new ImageListAdapter(UsageExampleAdapter.this, eatFoodyImages));
    }
}

这是适配器

   public class ImageListAdapter extends ArrayAdapter {  
    private Context context;
    private LayoutInflater inflater;

    private String[] imageUrls;

    public ImageListAdapter(Context context, String[] imageUrls) {
        super(context, R.layout.listview_item_image, imageUrls);

        this.context = context;
        this.imageUrls = imageUrls;

        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.listview_item_image, parent, false);
        }

        Picasso
            .with(context)
            .load(imageUrls[position])
            .fit() // will explain later
            .into((ImageView) convertView);

        return convertView;
    }
}
相关问题