-1

これが私のプログラムで、プログレスバーまたはプログレスダイアラグを追加する方法を教えてください

主な活動

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

xmlタグuitextviewおよび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(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.

これがAndroidでAsyncTaskを使用する方法のチュートリアルです。

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

AsyncTaskAndroidの例

そして、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 に答える