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:
Using java Syntax Highlighting
- package com.vijay.Splash;
- 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.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- public class MogoMenu extends ListActivity {
- ArrayList<String> linksArray = new ArrayList();
- ArrayList<String> itemsArray = new ArrayList();
- private static final int ACTIVITY_BOOKS=1;
- /** 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 onListItemClick(ListView l, View v, int position, long id) {
- // Pre-load the image then start the animation
- System.out.println("LISTVIEW" +getListView().toString());
- System.out.println("On Item Click" +position);
- if(position == 0)
- {
- DownloadBookShelf("http://xmlgwdev.lotusbookworks.com/?acctid=0bfHgPce7k:Hiw6hsB4fx&listID=1");
- //setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,itemsArray));
- Intent i = new Intent(this, ViewBook.class);
- i.putExtra("bookTitle", id);
- i.putStringArrayListExtra("bookLink",linksArray);
- startActivity(i);
- finish();
- }
- else if(position == 3)
- {
- setContentView(R.layout.help);
- }
- }
- 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*/
- private void DownloadBookShelf(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();
- }
- }
- }
Parsed in 0.057 seconds, using GeSHi 1.0.8.4



