1

I am trying to create a list of products with images of each product in a list view. The content is pulled from the web.

I have managed to create the list view with the name of each product and when each list item is clicked it shows the products image. However I want the images to show when the list is generated and I am not sure how to do this.

I would really appreciate any help. Heres my code for the activiy:

NodeList nodes = doc.getElementsByTagName("result");
    // Drawable d = null;
    for (int i = 0; i < nodes.getLength(); i++) {
        // HashMap<String, String> map = new HashMap<String, String>();
        HashMap<String, Object> map = new HashMap<String, Object>();



        Element e = (Element) nodes.item(i);

        map.put("id", "ID: " + XMLfunctions.getValue(e, "id"));
        map.put("name", "Name: " + XMLfunctions.getValue(e, "name"));

        Drawable d = dManager
        .fetchDrawable("http://MY_WEBSITE/images/"
                + XMLfunctions.getValue(e, "image"));

        map.put("image", d);
        map.put("imageUrl", XMLfunctions.getValue(e, "image"));
        mylist.add(map);
    }

    ListAdapter adapter = new SimpleAdapter(this, mylist,
            R.layout.products, new String[] { "image", "name", "id", },
            new int[] { R.id.item_image, R.id.item_title,
                    R.id.item_subtitle });

    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            @SuppressWarnings("unchecked")
            HashMap<String, Object> o = (HashMap<String, Object>) lv
                    .getItemAtPosition(position);
             //Toast.makeText(productsActivity.this, "ID '" + o.get("id") +
             //"' was clicked.", Toast.LENGTH_LONG).show();
            Drawable d = dManager
                    .fetchDrawable("http://MY_WEBSITE/images/"
                            + o.get("imageUrl"));
            ImageView imgView = (ImageView) view
                    .findViewById(R.id.item_image);
            o.put("image", d);
            imgView.setImageDrawable(d);
        }
    });
Ronny vdb
  • 2,324
  • 5
  • 32
  • 74

1 Answers1

0

The best way to display images in a Listview while downloading them in the background thread is fedors LazyList

coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118