【问题标题】:Read SD card image into ListView将 SD 卡图像读入 ListView
【发布时间】:2014-06-09 14:23:10
【问题描述】:

我设法在我的列表视图中使用了可绘制的图像,但我怎样才能对 SD 卡中的图像执行相同的操作?

ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

HashMap<String, String> map = new HashMap<String, String>();
map.put("KEY_IMAGE", Integer.toString(R.drawable.a)); //change to SD card
map.put("KEY_LINK", "link");
map.put("KEY_NAME", "name");
menuItems.add(map);

String[] from = { "KEY_IMAGE", "KEY_NAME", "KEY_LINK" };
int[] to =  {R.id.imageView_cell, R.id.list_headline,R.id.list_info };

SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), menuItems, R.layout.list_view,from, to);

listview.setAdapter(adapter);

【问题讨论】:

    标签: android listview hashmap


    【解决方案1】:

    您可以在不实现自定义适配器的情况下执行此操作。

    您应该实现SimpleAdapter.ViewBinder 接口,并调用setViewBinder() 来告诉您的适配器如何绑定数据。

    正如 LordRaydenMK 所提到的,在 setViewValue() 的实现中异步加载图像是一种很好的做法(例如,使用 Picasso)。

    【讨论】:

      【解决方案2】:

      您必须创建一个自定义适配器并覆盖getView 方法。您可以找到一个示例 here,使用视图回收和视图持有者的最佳做法。

      加载图像,尤其是在适配器中,应该在后台线程中完成,以免拖慢 UI。

      对于后台加载,您可以使用Picasso. 之类的库

      Picasso.with(context).load(new File(...)).into(imageView2);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多