Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在单击按钮时遍历XML节点_Java_Android_Xml_Iteration - Fatal编程技术网

Java 在单击按钮时遍历XML节点

Java 在单击按钮时遍历XML节点,java,android,xml,iteration,Java,Android,Xml,Iteration,我是Java编程的新手,但已经用c做了一些工作。我正在提取一些XML并成功地解析了它。我还能够成功地显示第一个节点或最后一个节点,但我需要在按下按钮时遍历XML节点 我一直在努力寻找一种在我的函数中完成迭代的方法,如下所示: public void buttonPressed(View view) { // All static variables final String URL = "http://info4420w.ad.uvu.edu/it442

我是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" />

提前感谢您的时间。

为什么不创建一个包含所有XML信息的ArrayList,比如说,一个MyObject所在的ArrayList,然后单击按钮时,只需将TextView设置为列表中的下一项?

这一行可能有问题:

TextView tButton=TextViewfindViewByIdR.id.button1

您是将按钮强制转换为文本视图还是该按钮实际上是文本视图

试试这个:


按钮tButton=ButtonfindViewByIdR.id.button1

如另一个答案中所述,将每个元素存储在ArrayList中,并在单击按钮时检索所需的索引。 尝试从手机上键入类似的内容,希望这对您有所帮助:

MyObject类:

然后在for循环中:


希望这有帮助,快乐编码

由于ArrayList还没有被覆盖,我不能这么做。
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


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