【问题标题】:Android SimpleAdapter image and text listAndroid SimpleAdapter 图片和文字列表
【发布时间】:2015-09-21 15:08:18
【问题描述】:

我目前正在尝试在 Android 上开发一个应用程序,它允许用户在 listView 中列出他们自己的对象。

我在将对象图像的 url 字符串转换为图像视图时遇到问题。

我已经成功从 JSON 中检索到对象 url,但是一旦我得到它,我不知道如何将它放在我的 imageView 中。

这是我的代码:

@Override
    protected String doInBackground(String... arg0) {
        try {

            JSONParser jParser = new JSONParser();
            JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
            dataJsonArr = json.getJSONArray("objects");

            //Création de la ArrayList qui nous permettra de remplire la listView
            listItem = new ArrayList<HashMap<String, String>>();

            // On parcour le JSON
            for (int i = 0; i < dataJsonArr.length(); i++) {
                JSONObject c = dataJsonArr.getJSONObject(i);

                // Formation des items de notre ListView
                map = new HashMap<String, String>();
                map.put("titre", c.getString("title"));
                map.put("price", c.getString("price"));
                map.put("addedDate", c.getString("addedDate"));
                map.put("img", c.getString("picture_url"));
                listItem.add(map);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String message) {

        Bitmap bmap = getBitmapFromURL(listItem.get(0).get("img").toString());
        image.setImageBitmap(bmap);

        //Création d'un SimpleAdapter qui se chargera de mettre les items présent dans notre list (listItem) dans la vue fragment_add_objet
        mSchedule = new SimpleAdapter (getActivity(), listItem, R.layout.layout_user_objects,
                new String[] {"img", "titre", "price", "addedDate"}, new int[] {R.id.img, R.id.titre, R.id.price, R.id.addedDate});

        //On attribut à notre listView l'adapter que l'on vient de créer
        maListViewPerso.setAdapter(mSchedule);
    }


}

【问题讨论】:

    标签: android json listview imageview simpleadapter


    【解决方案1】:

    您应该为 ListView 创建自己的自定义适配器。 It is pretty simple.

    要从 URL 获取图像,您可能需要使用 Picasso 库。在适配器的getView 中使用此库,您可以将此行添加到设置图像。

    Picasso.with(getContext())
           .load(mYourItems[position].getURL())
           .into(imageView);
    

    mYourItems 是这个适配器正在迭代的项目的集合。 position 也是当前行的索引。

    【讨论】:

      【解决方案2】:

      您应该使用 Picasso 或 Glide 之类的库从 url 加载图像。请记住,您必须先下载它才能在 ImageView 中显示它。 这让您的生活更轻松,但您始终可以在不使用库的情况下下载它,尽管我找不到原因。

      这里有一个使用毕加索的示例代码:

      Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
      

      您可以在这里查看:http://square.github.io/picasso/

      Glide 也是一个不错的选择,并且在使用方面非常相似。

      https://github.com/bumptech/glide

      还有更多,但我只是提到了我最喜欢的那些。

      另外,如果这是一个项目列表,您应该在适配器内部的 getView() 方法中执行此操作。

      【讨论】:

        【解决方案3】:

        我认为您应该创建自己的 BaseAdapter 实现,因为 SimpleAdapter 对于您的目标不够灵活。您可以使用一些第三方库从 URL 异步加载图像并将其插入到视图中。例如,您可以使用PicassoUIL

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-01-08
          • 2013-05-29
          • 2012-09-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-23
          相关资源
          最近更新 更多