开发者

How to display text &image after parsing in my application in android?

开发者 https://www.devze.com 2023-03-23 23:06 出处:网络
I am making an small application of webservices. My xml link is http://dl.dropbox.com/u/7215751/JavaCodeGeeks/AndroidFullAppTutorialPart03/Brad+Pitt.xml

I am making an small application of webservices. My xml link is http://dl.dropbox.com/u/7215751/JavaCodeGeeks/AndroidFullAppTutorialPart03/Brad+Pitt.xml

But i don't know how to display all the xml data, in my application . My code is

import java.util.ArrayList;

    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
        import com.javacodegeeks.android.apps.moviesearchapp.model.Image;
    import com.javacodegeeks.android.apps.moviesearchapp.model.Person;

    public class PersonHandler extends DefaultHandler {

        private StringBuffer buffer = new StringBuffer();

        private ArrayList<Person> personList;
        private Person person;
        private ArrayList<Image> personImagesList;      private Image personImage;

        @Override
        public void startElement(String namespaceURI, String localName,
                String qName, Attributes atts) throws SAXException {

            buffer.setLength(0);

            if (localName.equals("people")) {
                personList = new ArrayList<Person>();
            }
            else if (localName.equals("person")) {
                person = new Person();
            }
            else if (localName.equals("images")) {
                personImagesList = new ArrayList<Image>();
            }
            else if (localName.equals("image")) {
                personImage = new Image();
                personImage.type = atts.getValue("type");
                personImage.url = atts.getValue("url");
                personImage.size = atts.getValue("size");
                personImage.width = Integer.parseInt(atts.getValue("width"));
                personImage.height = Integer.parseInt(atts.getValue("height"));
            }

        }

        @Override
        public void endElement(String uri, String localName, String qName)throws SAXException {

            if (localName.equals("person")) {
                personList.add(person);
            }
            else if (localName.equals("score")) {
                person.score = buffer.toString();
            }
            else if (localName.equals("popularity")) {
                person.popularity = buffer.toString();
            }
            else if (localName.equals("name")) {
                person.name = buffer.toString();
            }
            else if (localName.equals("id")) {
                person.id = buffer.toString();
            }
            else if (localName.equals("biography")) {
                person.biography = buffer.toString();
            }
            else if (localName.equals("url")) {
                person.url = buffer.toString();
            }
            else if (localName.equals("version")) {
                person.version = buffer.toString();
            }
            else if (localName.equals("last_modified_at")) {
                person.lastModifiedAt = buffer.toString();
            }   
            else if (localName.equals("image")) {
                personImagesList.add(personImage);
            }   
            else if (localName.开发者_运维技巧equals("images")) {
                person.imagesList = personImagesList;
            }

        }

        @Override
        public void characters(char[] ch, int start, int length) {
            buffer.append(ch, start, length);
        }

        public ArrayList<Person> retrievePersonList() {
            return personList;
        }

    }

Can anybody plz help me to display images & Text inside Tags into my application.


Ok, here we go:

Assuming you have images stored in the drawable folder this could help you.

Resources res = getResources();
ImageView iv = new ImageView(getApplicationContext());
String temp = ; // Get name of the image from arraylist
String imageId = temp.replace(".jpg", "");
int image = res.getIdentifier(imageId, "drawable", getPackageName());
iv.setBackGroundResource(image);

Hope this helps you out :)

0

精彩评论

暂无评论...
验证码 换一张
取 消