2013-12-16 34 views
0

我是Java編程的新手,但已使用c#完成了一些工作。我正在拉一些XML併成功地解析了它,我也能夠成功顯示第一個節點或最後一個節點,但是我需要遍歷按鈕按下時的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

使用SAX解析器。 – vzamanillo

回答

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): 

希望這會有所幫助,快樂的編碼!

0

你爲什麼不創建一個ArrayList保存所有的XML信息(比方說,一個ArrayList<MyObject>其中MyObjectPOJO),那麼當按鈕被點擊剛纔設置的TextView s到列表中的下一個項目?

+0

ArrayList沒有被覆蓋,我不能那樣做。 –

0

可能是這一行的一個問題:

TextView tButton = (TextView)findViewById(R.id.button1);

你鑄造一個按鈕,一個TextView或按鈕實際上是一個TextView?

試試這個:

Button tButton = (Button)findViewById(R.id.button1);