【问题标题】:Bitmap in listview doesn't work [duplicate]列表视图中的位图不起作用[重复]
【发布时间】:2012-12-28 20:55:29
【问题描述】:

可能重复:
How do I do a lazy load of images in ListView

我有带有位图和文本的列表视图。当我下载图片并想查看它时,它不会出现在我的应用程序中。当我使用来自 R.drawable.imagename 的图像时,它可以工作...... 我的代码:

        List<HashMap<String, Object> > aList = new ArrayList<HashMap<String, Object> >();


    for(int i=0;i<ilosctalentow.size();i++){
        if (ilosctalentow.get(i).indexOf("0/")==-1)
        {
        HashMap<String, Object> hm = new HashMap<String,Object>();
        hm.put("txt", "xxx");
        hm.put("cur","Currency : " + ilosctalentow.get(i));
        Bitmap bmp = DownloadImage("http://www.xxx.pl/xxx/xxx/xhxuxj.png");
        hm.put("flag",bmp);
        aList.add(hm);

        Log.i(TAG,Integer.toString(R.drawable.afghanistan) );
        }
    }

    // Keys used in Hashmap
    String[] from = { "flag","txt","cur" };

    // Ids of views in listview_layout
    int[] to = { R.id.flag,R.id.txt,R.id.cur};

    // Instantiating an adapter to store each items
    // R.layout.listview_layout defines the layout of each item
    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);

    // Getting a reference to listview of main.xml layout file
    ListView listView = ( ListView ) findViewById(R.id.listview);

    // Setting the adapter to the listView Zaraz mnie huj strzeli
    listView.setAdapter(adapter);

请帮忙!

【问题讨论】:

  • 我怀疑是否可以从 UI 线程下载图像。您应该在后台线程中下载图像并在下载完成后更新适配器。另外我认为有必要创建自己的适配器而不是 SimpleAdapter 类。
  • 可以用绿色机器人制作吗?
  • 任何版本的 Android 都可以。这是著名的问题,您可以在那里找到代码示例:stackoverflow.com/questions/541966/…
  • @vorrtex:有可能,但这是个非常糟糕的主意..
  • @K-ballo 您在链接所指的页面上发布的所有启示。也许您可以在最佳答案中得分并说服所有人他们错了。

标签: java android image listview bitmap


【解决方案1】:

我想我可以发布一个指向另一个问题的链接作为答案,并解释如何将其应用于您当前的解决方案。

Lazy load of images in ListView

当您的应用程序启动时,显示一个没有图像的简单列表。然后启动一个后台线程来下载和创建位图。当线程完成其工作时 - 更新列表以显示加载的图像。

要完成这项工作,您应该创建一个自定义适配器。从this answer 中获取DrawableManager 类(因为接受的答案中的类包含一些错误)并像这样编写适配器:

// You should create your own class instead of TestItem
public class TestAdapter extends ArrayAdapter<TestItem> {

   private final LayoutInflater mInflater;
   private final DrawableBackgroundDownloader mDrawableBackgroundDownloader = new DrawableBackgroundDownloader();

    public TestAdapter(Context context, List<TestItem> objects) {
        super(context, -1, objects);
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final TestItem item = this.getItem(position);

        View view = convertView == null
                ? mInflater.inflate(R.layout.listview_layout, null)
                : convertView;

        TextView txtView = (TextView) view.findViewById(R.id.txt);
        TextView curView = (TextView) view.findViewById(R.id.cur);
        ImageView flagView = (ImageView) view.findViewById(R.id.flag);

        txtView.setText(item.getText());
        curView.setText(item.getCurrency());
        mDrawableBackgroundDownloader.loadDrawable(item.getFlagUrl(), flagView, null);

        return view;
    }
}

可以通过将找到的元素存储在视图包中或使用不同的类来下载和缓存图像来改进此适配器。但为了简单起见,我将保持原样。

【讨论】:

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