Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

While iterating display images

Hello Ben!!

Now situation is i have ArrayList, and with Iterator loop want to show images in my layout. Can it be possible in TextView. Please Suggest.

Code preview [code] Iterator<Product> it = products.iterator(); ImageView myImage = (ImageView) findViewById(R.id.imageView2); while(it.hasNext()) {

        Product currProduct  = it.next();
        String image = currProduct.image;

        myImage.setImageDrawable(getResources().getDrawable(image));
                    // Not working


        content = content + "\n\n\nName :" +  currProduct.name + "\n";
        content = content + "Position :" +  currProduct.position + "\n";
    }

[/code] Thanks!!

3 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

I had a lengthy discussion about this a while back with another student. Unfortunately Android doesn't include any default adapters for ListViews that display images. You need to build a custom adapter. It's easier than it sounds, though it's a little confusing without a good example. We will definitely cover it before too long.

Check out that forum post and see if it has any helpful information. I have to step away but wanted to give you a quick answer in case you start working on this soon. Let me know if you have any follow-up questions.

Hello Ben!!

Just followed your Custom Adapter code. and code is as follows :

private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
    {
        ArrayList<CustomObject> items = null;
        int eventType = parser.getEventType();
        CustomObject currentProduct = null;
        while (eventType != XmlPullParser.END_DOCUMENT){
            String name = null;
            switch (eventType){
                case XmlPullParser.START_DOCUMENT:
                    items = new ArrayList<CustomObject>();
                    break;
                case XmlPullParser.START_TAG:
                    name = parser.getName();
                    if (name.equals("member")){
                        currentProduct = new CustomObject();
                    } else if (currentProduct != null){
                        if (name.equals("name")){
                            currentProduct.name = parser.nextText();
                        } else if (name.equals("position")){
                            currentProduct.position = parser.nextText();
                        } else if (name.equals("image")){
                            currentProduct.image= parser.nextText();
                        }
                    }
                    Log.i(TAG, "Node parsing");
                    break;
                case XmlPullParser.END_TAG:
                    name = parser.getName();
                    if (name.equalsIgnoreCase("member") && currentProduct != null){
                        items.add(currentProduct);
                    }
            }
            eventType = parser.next();
        }

        CustomAdapter adapter = new CustomAdapter(DepartmentActivity.this, items);  // Error Here
//Intellisense suggests to change type of 'items' to 'ArrayList<CustomObject>'

        setListAdapter(adapter);

      //  printProducts(items);
        Log.i(TAG, "XML parsed " + items.size());
    }

i believe its already. Please guide from here. I am stucked. If u another souce code copy (Can forward u?)

Thanks :) u Rox

Ben Jakuben
Ben Jakuben
Treehouse Teacher

I'm on holiday this week, but I'll try to take a look when I have some time!

Whole code link for your reference.

http://jsfiddle.net/navdeep/PZdgN/

Please check it. Thanks a lot!!