Wednesday, April 27, 2011

[android-developers] How to change TextView object.

I want to take the TextView in the xml file.So in this project i want
convert my object to array type.

package com.assign.ment;

import java.net.URL;

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

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

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class agrawal_first extends Activity {
Button first,second;
String st="Rishabh";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);

/** Create a new textview array to display the results */
TextView name[];

TextView website[];
TextView category[];

try {

URL url = new URL(
"http://meherwin7.99k.org/assignment.xml");
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();

NodeList nodeList =
doc.getElementsByTagName("CityWeatherForecast");

/** Assign textview array lenght by arraylist size */
name = new TextView[nodeList.getLength()];
website = new TextView[nodeList.getLength()];
category = new TextView[nodeList.getLength()];

for (int i = 0; i < nodeList.getLength(); i++) {

Node node = nodeList.item(i);

name[i] = new TextView(this);
website[i] = new TextView(this);
category[i] = new TextView(this);

Element fstElmnt = (Element) node;
NodeList nameList =
fstElmnt.getElementsByTagName("BulletinName");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
name[i].setText("BulletinName = "
+ ((Node) nameList.item(0)).getNodeValue());

NodeList websiteList =
fstElmnt.getElementsByTagName("BulletinType");
Element websiteElement = (Element) websiteList.item(0);
websiteList = websiteElement.getChildNodes();
website[i].setText("BulletinType = "
+ ((Node) websiteList.item(0)).getNodeValue());

NodeList aList =
fstElmnt.getElementsByTagName("BulletinProvider");
Element aElement = (Element) aList.item(0);
aList = aElement.getChildNodes();
category[i].setText("BulletinProvider = "
+ ((Node) aList.item(0)).getNodeValue().toUpperCase());

layout.addView(name[i]);
layout.addView(website[i]);
layout.addView(category[i]);

}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}

/** Set the layout view to display */
setContentView(layout);
setContentView(R.layout.main_first);

}
}

How it is possible.Please correct it

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

No comments:

Post a Comment