0

可能重复:
如何在 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);

请帮忙!

4

1 回答 1

0

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

ListView 中图像的延迟加载

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

要完成这项工作,您应该创建一个自定义适配器。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;
    }
}

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

于 2012-12-28T23:27:07.553 回答