我是Java编程的新手,但已经用c#做了一些工作。我正在提取一些XML并成功解析它我也能够成功显示第一个节点或最后一个节点,但是我需要按下按钮来遍历XML节点。
我仍在试图找到一种方法来完成我的函数中的迭代,如下所示:
public void buttonPressed(View view)
{
// All static variables
final String URL = "http://info4420w.ad.uvu.edu/it4420/10540595/Final_Project/Final_Project/quoterestservice.aspx";
// XML node keys
final String KEY_TABLE1 = "Table1"; // parent node
final String KEY_QUOTE_ID = "QuoteID";
final String KEY_CUST_NAME = "custName";
final String KEY_CUST_PHONE = "custPhone";
final String KEY_CUST_YEAR = "custYear";
final String KEY_CUST_MAKE = "custMake";
final String KEY_CUST_MODEL = "custModel";
final String KEY_CUST_ISSUE = "custIssue";
final String KEY_CUST_LOCLAT = "custLocLat";
final String KEY_CUST_LOCLNG = "custLocLng";
final String KEY_CUST_DESTLAT = "custDestLat";
final String KEY_CUSTDESTLNG = "custDestLng";
String quoteID = new String();
String cName = new String();
String cPhone = new String();
String cYear = new String();
String cMake = new String();
String cModel = new String();
String cIssue = new String();
String cLocLat = new String();
String cLocLng = new String();
String cDestLat = new String();
String cDestLng = new String();
try {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_TABLE1);
quoteID = "";
cName = "";
cPhone = "";
cYear = "";
cMake = "";
cModel = "";
cIssue = "";
cLocLat = "";
cLocLng = "";
cDestLat = "";
cDestLng = "";
// looping through all Table1 nodes <Table1>
// for (int i = 0; i < nl.getLength(); i++) {
int i = 0;
Element e = (Element) nl.item(i);
quoteID = parser.getValue(e, KEY_QUOTE_ID); // name child value
cName = parser.getValue(e, KEY_CUST_NAME); // cost child value
cPhone = parser.getValue(e, KEY_CUST_PHONE); // description child value
cYear = parser.getValue(e, KEY_CUST_YEAR);
cMake = parser.getValue(e, KEY_CUST_MAKE);
cModel = parser.getValue(e, KEY_CUST_MODEL);
cIssue = parser.getValue(e, KEY_CUST_ISSUE);
cLocLat = parser.getValue(e, KEY_CUST_LOCLAT);
cLocLng = parser.getValue(e, KEY_CUST_LOCLNG);
cDestLat = parser.getValue(e, KEY_CUST_DESTLAT);
cDestLng = parser.getValue(e, KEY_CUSTDESTLNG);
i++;
// }
} catch (Exception e) {
cName = e.getMessage() + e.getStackTrace();
}
TextView tQuote = (TextView)findViewById(R.id.textView1);
TextView tCust = (TextView)findViewById(R.id.textViewCust);
TextView tIssue = (TextView)findViewById(R.id.textViewIssue);
TextView tCar = (TextView)findViewById(R.id.textViewCar);
TextView tLoc = (TextView)findViewById(R.id.textViewLoc);
TextView tButton = (TextView)findViewById(R.id.button1);
tButton.setText("Next Quote");
tQuote.setText("Quote ID: " + quoteID );
tCust.setText("Name: " + cName + " Cell: " + cPhone);
tIssue.setText("Issue: " + cIssue);
tCar.setText("Make: " + cMake + " Model: " + cModel + " Year: " + cYear);
tLoc.setText("Loc: " + cLocLat + ", " + cLocLng);
}
我的按钮:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewLoc"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:onClick="buttonPressed"
android:text="Get Quote" />
提前感谢您的时间。
答案 0 :(得分:0)
为什么不创建一个ArrayList
来保存所有XML信息(例如,ArrayList<MyObject>
MyObject
是POJO)然后点击按钮只需将TextView
设置为列表中的下一个项目吗?
答案 1 :(得分:0)
这条线可能有问题:
TextView tButton = (TextView)findViewById(R.id.button1);
您是在向TextView投射按钮还是按钮实际上是TextView?
试试这个:
Button tButton = (Button)findViewById(R.id.button1);
答案 2 :(得分:0)
如另一个答案中所述,将每个元素存储在ArrayList
中,并在按钮单击时检索所需的索引。
试试这样的事情(从手机打字,希望这有帮助):
MyObject类:
public class MyObject {
public String quoteID;
public String name;
public String phone;
public int year;
//...the rest of any information you want stored in this object
}
然后在您的for循环中:
// Create a new ArrayList of MyObject
ArrayList<MyObject> elements = new ArrayList<MyObject>();
try {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_TABLE1);
quoteID = "";
cName = "";
cPhone = "";
cYear = "";
cMake = "";
cModel = "";
cIssue = "";
cLocLat = "";
cLocLng = "";
cDestLat = "";
cDestLng = "";
// looping through all Table1 nodes <Table1>
for (int i = 0; i < nl.getLength(); i++) {
// in your for-loop, create a new MyObject
MyObject myObject = new MyObject();
Element e = (Element) nl.item(i);
quoteID = parser.getValue(e, KEY_QUOTE_ID); // name child value
// set String objects of MyObject instance myObject
myObject.quoteID = quoteID;
cName = parser.getValue(e, KEY_CUST_NAME); // cost child value
myObject.name = cName;
// pass rest of info to myObject with below values from parser...
cPhone = parser.getValue(e, KEY_CUST_PHONE); // description child value
cYear = parser.getValue(e, KEY_CUST_YEAR);
cMake = parser.getValue(e, KEY_CUST_MAKE);
cModel = parser.getValue(e, KEY_CUST_MODEL);
cIssue = parser.getValue(e, KEY_CUST_ISSUE);
cLocLat = parser.getValue(e, KEY_CUST_LOCLAT);
cLocLng = parser.getValue(e, KEY_CUST_LOCLNG);
cDestLat = parser.getValue(e, KEY_CUST_DESTLAT);
cDestLng = parser.getValue(e, KEY_CUSTDESTLNG);
//then finally, add myObject to ArrayList
elements.add(myObject);
i++;
// }
} catch (Exception e) {
cName = e.getMessage() + e.getStackTrace();
}
TextView tQuote = (TextView)findViewById(R.id.textView1);
TextView tCust = (TextView)findViewById(R.id.textViewCust);
TextView tIssue = (TextView)findViewById(R.id.textViewIssue);
TextView tCar = (TextView)findViewById(R.id.textViewCar);
TextView tLoc = (TextView)findViewById(R.id.textViewLoc);
TextView tButton = (TextView)findViewById(R.id.button1);
tButton.setText("Next Quote");
tQuote.setText("Quote ID: " + quoteID );
tCust.setText("Name: " + cName + " Cell: " + cPhone);
tIssue.setText("Issue: " + cIssue);
tCar.setText("Make: " + cMake + " Model: " + cModel + " Year: " + cYear);
tLoc.setText("Loc: " + cLocLat + ", " + cLocLng);
}
最后,它可以通过索引值检索:
MyObject myObject = elements.get(index):
希望这有帮助,快乐编码!