1

\Android プログラミングでは..xmltags のデータを url(xmlparsing) から取得するのが好きですが、その間、データが xmltags から取得されるまで進行状況をロードしたいと考えています。コードを添付して、儀式か間違っているかを確認します\

 \\main activity\\


public class act extends Activity
{

 \\button used to start the function after the click\\

    Button b;
    public ProgressDialog mDialog;
    @Override
     public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        b=(Button)findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                new GetTask().execute();

            }
        });
}
   \\my asynctask\\

class GetTask extends AsyncTask<Object, Void, String>
{



    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();

         mDialog = new ProgressDialog(act.this);
         mDialog.setMessage("Please wait...");
         mDialog.show();
    }
     @Override
    protected String doInBackground(Object... params)
    {

  \\ui programetically declare the textviews\\

                 try {
                    LinearLayout layout = new LinearLayout(act.this);
                    layout.setOrientation(1);
                    TextView no[];
                    TextView na[];
                    TextView c[];


                  URL url = new URL("http://api.androidhive.info/pizza/?format=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]);
                      setContentView(layout);

                      }} 
             catch (Exception e) {
                     }
        return null;

            }

            @Override
    protected void onPostExecute(String result)
    {
        super.onPostExecute(result);
        mDialog.dismiss(); 

            }
   }
  }
4

3 に答える 3

0

まず、doInBackground() で UI 関連のアクションを作成できません。

必要に応じて、doInBackground() プロセスが完了した後に onPostExecute() で実行できます...

だからあなたの論理を変えてください..

于 2013-01-24T06:16:57.367 に答える
0
NodeList namelist;
String[] no,na,c;
 try {
              URL url = new URL("http://api.androidhive.info/pizza/?format=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 String[nodeList.getLength()];
              na = new String[nodeList.getLength()];
              c = new String[nodeList.getLength()];
              for (int i = 0; i < nodeList.getLength(); i++) {
                  Node node = nodeList.item(i);

                  Element fstElmnt = (Element) node;

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

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

                  NodeList costlist = fstElmnt.getElementsByTagName("cost");
                  Element costlement = (Element) costlist.item(0);
                  costlist = costlement.getChildNodes();


                  }} 
         catch (Exception e) {
                 }
    return null;

        }

        @Override
protected void onPostExecute(String result)
{
    super.onPostExecute(result);
                LinearLayout layout = new LinearLayout(act.this);
                layout.setOrientation(1);
                TextView no[];
                TextView na[];
                TextView c[];
              no = new TextView[nodeList.getLength()];
              na = new TextView[nodeList.getLength()];
              c = new TextView[nodeList.getLength()];
              for (int i = 0; i < nodeList.getLength(); i++) {

                  no[i] = new TextView(act.this);
                  na[i] = new TextView(act.this);
                  c[i] = new TextView(act.this);
                  no[i].setText("ID="+no[i] );
                 na[i].setText("pizza name="+ na[i]);
                c[i].setText("cost="+ ((Node) costlist.item(0)).getNodeValue());

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

        }
于 2013-01-24T07:34:55.090 に答える
0

次のコードは間違っています。

onPostExecute() にはデータがないので、表示したいものは何でも doInBackground() の代わりに onPostExecute() で表示するだけです。

だからそれを変更..

LazyLoading または Asynctask アプリで見つけることができるサンプル例:-

https://github.com/thest1/LazyList

https://github.com/ethervoid/android-async-task-example

次のコードであなたの答えを得ることができます...!!!!

于 2013-01-24T06:27:23.930 に答える