1

I am pretty new to android studio and have a question that I can't seem to solve. I am parsing some XML from a website using a for loop to get each element. My question is how do I pause it on the first iteration through the for loop, so that it won't continue to the next until this button is clicked?

Here is my for loop:

try {
            URL url = new URL("http://www.website.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getElementsByTagName("patient_order");
            /** Assign textview array length by arraylist size */
            patientName = new TextView[nodeList.getLength()];
            patientID = new TextView[nodeList.getLength()];
            medicine = new TextView[nodeList.getLength()];
            dosage = new TextView[nodeList.getLength()];
            refillsRemaining = new TextView[nodeList.getLength()];
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                patientName[i] = new TextView(this);
                patientID[i] = new TextView(this);
                medicine[i] = new TextView(this);
                dosage[i] = new TextView(this);
                refillsRemaining[i] = new TextView(this);
                Element fstElmnt = (Element) node;
                NodeList nameList = fstElmnt.getElementsByTagName("patientName");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();
                patientName[i].setText("Patient: "
                        + ((Node) nameList.item(0)).getNodeValue());
                NodeList websiteList = fstElmnt.getElementsByTagName("patientID");
                Element websiteElement = (Element) websiteList.item(0);
                websiteList = websiteElement.getChildNodes();
                patientID[i].setText("ID: "
                        + ((Node) websiteList.item(0)).getNodeValue());
                NodeList medicineList = fstElmnt.getElementsByTagName("medicine");
                Element medicineElement = (Element) medicineList.item(0);
                medicineList = medicineElement.getChildNodes();
                medicine[i].setText("RX: "
                        + ((Node) medicineList.item(0)).getNodeValue());
                NodeList dosageList = fstElmnt.getElementsByTagName("dosage");
                Element dosageElement = (Element) dosageList.item(0);
                dosageList = dosageElement.getChildNodes();
                dosage[i].setText("Dosage: "
                        + ((Node) dosageList.item(0)).getNodeValue());
                NodeList refillsList = fstElmnt.getElementsByTagName("refillsRemaining");
                Element refillsElement = (Element) refillsList.item(0);
                refillsList = refillsElement.getChildNodes();
                refillsRemaining[i].setText("Remaining: "
                        + ((Node) refillsList.item(0)).getNodeValue());
                layout.addView(patientName[i]);
                layout.addView(patientID[i]);
                layout.addView(medicine[i]);
                layout.addView(dosage[i]);
                layout.addView(refillsRemaining[i]);
            btn.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View view)
                {
                   //What is going to happen to make the for loop continue 
                }
            });
            }//end for
        } //end try
joao2fast4u
  • 6,868
  • 5
  • 28
  • 42
Shawn
  • 2,355
  • 14
  • 48
  • 98
  • In Java, similar situation need to be handled in two threads. One in the event thread, and the app logic in a new thread, and you need a volatile variable to share between the threads, and you can check the value of the variable to take the decision to proceed or not – spiderman Apr 29 '14 at 21:32

2 Answers2

0

You never want to pause your UI thread so create a new thread to execute your for loop in. This link http://developer.android.com/training/multiple-threads/communicate-ui.html should spell out a few options you have for communication between your main UI thread (which must not be jammed up at all costs) and your side thread doing the actual work. Your main UI thread is busy keeping the screen active and responsive, so pausing it is generally a no-no.

jeffrey
  • 965
  • 14
  • 25
0

You can use approach from this answer: How to Pause and Resume a Thread in Java from another Thread. But it doesn't work in your case

Because

  1. Never pause the UI thread. It hangs your UI and leads to ANR. And never use network and other heavyweight operation in the UI thread either. You can use AyncTask for doing hard work. If you wrap your XML parsing code in AsyncTask you can start it in onClick() method. So you don't need to pause loop
  2. Do you really need array of TextView's? If you need to show the list of data why not to use ListView?
Community
  • 1
  • 1
olegr
  • 1,999
  • 18
  • 23