Hi,
I am creating an application which parses an XML from the web and
displays the items of a <title>tag in another ListView. Basically i
want to know an effecient way of using ListView all throughout the
application.

Ex:
Part
1
Part 2                                                Part 3
My Bookshelf -----Click----->Opens another ListView ---->Book 1-------
> Click------> ListView----->First Page
Options
Book 2
Help
Exit

I am able to go from part 1 to part 2. but not from part 2 to part3. I
might be doing some mistake in part 1 to part 2 itself.

Please advise. I am very confused and stuck.

My code:

package com.vijay.MogoMenu;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;


public class MogoMenu extends ListActivity      {


        ArrayList linksArray = new ArrayList();
        ArrayList itemsArray = new ArrayList();



        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1, MENU_NAMES));
    }

    private static final String[] MENU_NAMES = new String[] {
        "My Bookshelf",
        "On the Go",
        "Options",
        "Help",
    };

    public void onItemClick(AdapterView parent, View v, int position,
long id) {
        // Pre-load the image then start the animation
        System.out.println("On Item Click" +position);
        if(position == 0)
        {
                DownloadRSS("http://xmlgwdev.lotusbookworks.com/?
acctid=0bfHgPce7k:Hiw6hsB4fx&listID=1");
        }
        else if(position == 3)
        {
                setContentView(R.layout.help);
        }


    }

    public void onClick(View v) {
        System.out.println("On View Click");
    }

    public InputStream OpenHttpConnection(String urlString) throws
IOException
    {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();
        System.out.println("Opening Connection");
        if (!(conn instanceof HttpURLConnection))
            throw new IOException("Not an HTTP connection");

        try
        {
                System.out.println("Obtaining CONNECTION");
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            System.out.println("Connecting");
            httpConn.connect();

            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK)
            {
                System.out.println("SUPER CONNECTION");
                in = httpConn.getInputStream();
                System.out.println("Bitmap puttin into IMG1");
            }
        }
        catch (Exception ex)
        {
            throw new IOException("Error connecting");
        }
        return in;
    }


    /*Download XML RSS feed*/
        @SuppressWarnings("unchecked")
        private void DownloadRSS(String URL)
    {
        InputStream in = null;
        try {
            in = OpenHttpConnection(URL);
            Document doc = null;
            DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
            DocumentBuilder db;

            try {
                db = dbf.newDocumentBuilder();
                doc = db.parse(in);
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            doc.getDocumentElement().normalize();
            itemsArray.clear();
            linksArray.clear();


            String strTitle = null;
            String strLink = null;

            NodeList itemNodesList = doc.getElementsByTagName("a");

            for (int i = 0; i < itemNodesList.getLength(); i++) {
                Node itemNode = itemNodesList.item(i);
                if (itemNode.getNodeType() == Node.ELEMENT_NODE)
                {
                    //---convert the Node into an Element---
                    Element itemElement = (Element) itemNode;

                    //---get all the TITLES element under the
                    NodeList titleNodes =
                        (itemElement).getElementsByTagName("a");

                    Element titleElement = (Element) titleNodes.item
(0);

                    //---get all the child nodes under the <title>
element---
                    NodeList textitemNodes =
                        ((Node) titleElement).getChildNodes();

                    //---retrieve the text of the <title> element---
                    strTitle = ((Node) textitemNodes.item
(0)).getNodeValue();
                    itemsArray.add(strTitle);
                    System.out.println("ITEM : "+strTitle);
                }
            }

            for (int i = 0; i < itemNodesList.getLength(); i++) {
                        strLink = ((Element) itemNodesList.item(i)).getAttribute
("href");
                    linksArray.add(strTitle);
                    System.out.println("URL LINK: "+strLink);
           }


            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        setListAdapter(new ArrayAdapter
(this,android.R.layout.simple_list_item_1,itemsArray));
    }


}


Thanks,
Vijay.C
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to