簡體   English   中英

android listview從url加載圖像到imageview

[英]android listview loading image onto imageview from url

我想在我的列表視圖中顯示圖像

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

}

}

我該如何為列表視圖添加圖像並顯示它? 任何對此的反饋將不勝感激。 謝謝

這是您可以根據需要修改的代碼

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

用法示例活動類

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM