-1

Here is my program and tell me how to add progress bar or progress dialag

the main activity

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 try {
        LinearLayout layout = new LinearLayout(act.this);
        layout.setOrientation(1);

for retrieving the number,name and cost from the xml tag ui textview and url declaration\

        TextView no[];
        TextView na[];
        TextView c[];
           setContentView(layout);

The url is declared here

  URL url = new URL("http://api.androidhive.info/pizza/?format=xml"); 

the xml tag which i retrieve the data

      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();          
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(new InputSource(url.openStream()));
      doc.getDocumentElement().normalize();
      NodeList nodeList = doc.getElementsByTagName("item");
      no = new TextView[nodeList.getLength()];
      na = new TextView[nodeList.getLength()];
      c = new TextView[nodeList.getLength()];

      for (int i = 0; i < nodeList.getLength(); i++) {
          Node node = nodeList.item(i);
          no[i] = new TextView(act.this);
          na[i] = new TextView(act.this);
          c[i] = new TextView(act.this);
          Element fstElmnt = (Element) node;

          NodeList idlist = fstElmnt.getElementsByTagName("id");
          Element numelement = (Element) idlist.item(0);
          idlist = numelement.getChildNodes();
          no[i].setText("ID="+ ((Node) idlist.item(0)).getNodeValue());

          NodeList namelist = fstElmnt.getElementsByTagName("name");
          Element namelement = (Element) namelist.item(0);
          namelist = namelement.getChildNodes();
          na[i].setText("pizza name="+ ((Node) namelist.item(0)).getNodeValue());

          NodeList costlist = fstElmnt.getElementsByTagName("cost");
          Element costlement = (Element) costlist.item(0);
          costlist = costlement.getChildNodes();
          c[i].setText("cost="+ ((Node) costlist.item(0)).getNodeValue());

          layout.addView(no[i]);
          layout.addView(na[i]);
          layout.addView(c[i]);


          }} 
 catch (Exception e) {
         }


 }}

end of program

Narendra Pal
  • 6,474
  • 13
  • 49
  • 85
Androider5008
  • 85
  • 1
  • 7

3 Answers3

1
public class MainActivity extends Activity{

TextView no[];
TextView na[];
TextView c[];
LinearLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    layout = new LinearLayout(MainActivity.this);
    setContentView(layout);
    MyTask myTask = new MyTask();
    myTask.execute();
}

public class MyTask extends AsyncTask<Void , Void , Void>{
    NodeList costlist;
    Element costlement;
    NodeList idlist;
    NodeList namelist ;
    int i=0;
    @Override
    protected Void doInBackground(Void... params){
        try{
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            URL url = new URL("http://api.androidhive.info/pizza/?format=xml");
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getElementsByTagName("item");
            for (i = 0; i < nodeList.getLength(); i ++ ){
                Node node = nodeList.item(i);

                Element fstElmnt = (Element)node;
                idlist = fstElmnt.getElementsByTagName("id");
                Element numelement = (Element)idlist.item(0);
                idlist = numelement.getChildNodes();
                ;

                namelist = fstElmnt.getElementsByTagName("name");
                Element namelement = (Element)namelist.item(0);
                namelist = namelement.getChildNodes();

                costlist = fstElmnt.getElementsByTagName("cost");
                costlement = (Element)costlist.item(0);
                Void a = null;// just let it be ;
                publishProgress(a);

            }
        }catch (DOMException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (ParserConfigurationException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (SAXException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values){
        super.onProgressUpdate(values);
        no[i] = new TextView(MainActivity.this);
        na[i] = new TextView(MainActivity.this);
        c[i] = new TextView(MainActivity.this);
        no[i].setText("ID=" + ((Node)idlist.item(0)).getNodeValue());
        na[i].setText("pizza name=" + ((Node)namelist.item(0)).getNodeValue());
        costlist = costlement.getChildNodes();
        c[i].setText("cost=" + ((Node)costlist.item(0)).getNodeValue());
        layout.addView(no[i]);
        layout.addView(na[i]);
        layout.addView(c[i]);
    }
    @Override
    protected void onPreExecute(){
        // TODO Auto-generated method stub
        super.onPreExecute();
    }
}

}

0

Yes you can do this using the AsyncTask in android

There are 4 methods in AsyncTask

  1. onPreExecute() It invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.

2.doInBackground(Params...) invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use

3.publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

4.onProgressUpdate(Progress...),

 invoked on the UI thread after a call to 
publishProgress(Progress...). The timing of the execution is undefined.
 This method is used to display any form of progress in the user interface while
 the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.

5.onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

Here is the tutoriasl of How to use AsyncTask in android.

http://labs.makemachine.net/2010/05/android-asynctask-example/

AsyncTask Android example

And the below link for the AsyncTask from android developer site.

http://developer.android.com/reference/android/os/AsyncTask.html

For Detail info. must visit the Developer site montioned above

Community
  • 1
  • 1
Narendra Pal
  • 6,474
  • 13
  • 49
  • 85
  • ok in AsyncTask if i can able to add this code in doinBackground.if yes means explain pls. – Androider5008 Jan 24 '13 at 12:10
  • yes you can add all your data retriving code in the doInBackground(). – Narendra Pal Jan 24 '13 at 12:11
  • sure "http://stackoverflow.com/questions/14494913/in-android-this-program-not-retrieve-the-xml-data-after-the-progress-bar-loading/14495207#comment20201865_14495207" see this link narendra pal sir – Androider5008 Jan 24 '13 at 12:20
0

Hi use this AsyncTask method

public class GetTask extends AsyncTask<Void, Void, Integer> {
    private ProgressDialog mProgress;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        mProgress = ProgressDialog.show(MainActivity.this, "", "Loading");
    }

    @Override
    protected Integer doInBackground(Void... params) {
        // TODO Auto-generated method stub
        // Do your stuff 
    }

    @Override
    protected void onPostExecute(Integer result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        if (mProgress.isShowing())
            mProgress.cancel();

    }

}
MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22