-1

这是我的程序,告诉我如何添加进度条或进度对话框

主要活动

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

用于从 xml 标签ui textview 和 url 声明中检索数字、名称和成本\

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

url 在这里声明

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

我检索数据的 xml 标记

      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) {
         }


 }}

程序结束

4

3 回答 3

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();
    }
}

}

于 2013-01-24T12:24:16.487 回答
0

是的,您可以使用android中的AsyncTask执行此操作

AsyncTask中有4个方法

  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 (参数...) 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.发布进度(进度...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

4. onProgressUpdate(进度...),

 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(结果) ,invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

这是如何在android中使用AsyncTask的教程。

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

AsyncTask Android 示例

以及来自 android 开发者网站的 AsyncTask 的以下链接。

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

有关详细信息。必须访问上面提到的开发者网站

于 2013-01-24T12:08:39.183 回答
0

你好用这个AsyncTask方法

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();

    }

}
于 2013-01-24T12:15:06.147 回答