0

Possible Duplicate:
How do I do a lazy load of images in ListView

i have listview with bitmap and text. When i download picture and i want to view it, it not appears in my app. When i used image from R.drawable.imagename then it works... My code:

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

Please help!

Community
  • 1
  • 1
  • I doubt that it is possible to download images from the UI thread. You should download images in a background thread and update the adapter when the downloading is finished. Also I think it will be necessary to create your own adapter instead of the SimpleAdapter class. – vortexwolf Dec 28 '12 at 21:05
  • is it possible to make it in green droid? – user1906882 Dec 28 '12 at 22:06
  • It is possible with any version of Android. Here is the famous question, you can find code samples there: http://stackoverflow.com/questions/541966/how-do-i-do-a-lazy-load-of-images-in-listview – vortexwolf Dec 28 '12 at 22:08
  • @vorrtex: It is possible, its a very bad idea though.. – K-ballo Dec 28 '12 at 22:24
  • @K-ballo All you revelations post on the page to which the link refers. Maybe you can outscore top answers and convince everyone that they are wrong. – vortexwolf Dec 28 '12 at 22:44
  • @vorrtex: Sorry for not being clear, what I meant to be a bad idea is downloading images from the UI thread – K-ballo Dec 28 '12 at 22:45
  • @K-ballo As far as I know this is forbidden now and the application will throw an exception. So downloading from the UI thread should be impossible. http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – vortexwolf Dec 28 '12 at 22:57
  • Why when i used mStrings = urltalentow.toArray(new String[urltalentow.size()]); images doesn't work but private String[] mStrings={"http://xxx/611.png","http://xxx611.png"}; work? – user1906882 Dec 29 '12 at 17:12

1 Answers1

0

I think I can post a link to another question as an answer and explain how to apply it to your current solution.

Lazy load of images in ListView

When your application starts, display a simple list without images. Then start a background thread which downloads and creates bitmaps. When the thread completes its job - update the list so that it displays loaded images.

To make this work, you should create a custom adapter. Take the DrawableManager class from this answer (because the class from the accepted answer contains some bugs) and write the adapter like this:

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

This adapter can be improved by storing found elements in a view bag or using different class for downloading and caching of images. But I will leave it as it is for the sake of simplicity.

Community
  • 1
  • 1
vortexwolf
  • 13,967
  • 2
  • 54
  • 72