Mike Johns
Mike Johns

Reputation: 21

pulling information from xml but wont display

Nothing is getting written to the TextView screen.

I'm not sure what I'm doing wrong here but this is my following code:

<?xml version="1.0"?> 
 <news> 
  <article title="TITLE HERE" link="*.php?showtopic=118" date="DATE HERE"></article> 
 </news>

JAVA CODE:

try {
    //open an URL connection make GET to the server and
    URL url = new URL("http://.../m_news.xml");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
    InputStream is = conn.getInputStream();

    //DocumentBuilderFactory, DocumentBuilder are used for
    //xml parsing
    DocumentBuilderFactory dbf = DocumentBuilderFactory
        .newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    //using db (Document Builder) parse xml data and assign
    //it to Element
    Document document = db.parse(is);
    Element element = document.getDocumentElement();

    NodeList nodeList = element.getElementsByTagName("news");
    NodeList nodeList2 = element.getElementsByTagName("article");

        if (nodeList.getLength() > 0) {
            for (int i = 0; i < nodeList2.getLength(); i++) {
                Element entry = (Element) nodeList.item(0); 

                Element _rowE = (Element) entry.getElementsByTagName("article").item(i);  
                Node _rowTITLE = _rowE.getAttributes().getNamedItem("title");
                Node _rowLINK = _rowE.getAttributes().getNamedItem("link");

                TextView textview = new TextView(this);
                textview.setMovementMethod(LinkMovementMethod.getInstance());
                String text = "<a href='"+_rowLINK.getNodeValue()+"'>"+_rowTITLE.getNodeValue()+"</a>";
                textview.setText(Html.fromHtml(text));
                setContentView(textview);
            }
        }    
    }
} catch (Exception e) {
    e.printStackTrace();
}

Upvotes: 2

Views: 180

Answers (1)

Reuben Scratton
Reuben Scratton

Reputation: 38707

You are instantiating TextViews and calling setContentView() with each of them... this is never going to work. At the very least you need to give some layout instructions (i.e. call setLayoutParams on each TextView) but I'm not even sure that will work without some kind of container object.

Try this. Create a simple layout called news.xml which has this:

<LinearLayout
    android:id="@+id/newsItems"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
/>

Then change your code to include the following:

setContentView(R.layout.news);
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.newsItems);
...
for (int i = 0; i < nodeList2.getLength(); i++) {
    ...
    TextView textview = new TextView(this);
    textview.setLayoutParams(new LinearLayout.LayoutParams(FILL_PARENT, WRAP_CONTENT));
    ...
    textview.setText(Html.fromHtml(text));
    linearLayout.addView(textview);
}

Upvotes: 1

Related Questions