for as循环内部异步

时间:2013-01-12 13:11:34

标签: android for-loop android-asynctask progressdialog

需要帮助从task2以后更具体地查看代码,这是错误的来源,但我无法弄清楚。假设对于循环不建议是否有任何其他替代方案,我想要的代码?除了这个getInformations之外,该类实际上还调用了其他2个我没有上传的类。这就是为什么我需要返回drawable。谢谢你

private AsyncTask<String, Void, String> task;
private AsyncTask<String, Void, String> task1;
private AsyncTask<String, Void, Drawable[]> task2;
public void getInformations(){
    task = new AsyncTask<String, Void, String>() {
        ProgressDialog dialog;
        Bundle bundle;
        String res;
        @Override 
        protected void onPreExecute(){
            dialog = new ProgressDialog(Thoughts.this, ProgressDialog.STYLE_SPINNER);
            dialog.setMessage("Loading Data...");           
            dialog.setCancelable(false);
            dialog.setCanceledOnTouchOutside(false);    

            dialog.show();
        }

        @Override
        protected String doInBackground(String... arg0) {
            bundle = getIntent().getExtras();
            res = bundle.getString("Username");
            return res;
        }

        @Override
        protected void onPostExecute(String result) {
            dialog.dismiss();
            username = result;      

            task1 = new AsyncTask<String, Void, String>() {
                ProgressDialog dialog1;
                ArrayList<NameValuePair> postParameters;
                String response1;
                String res1;
                @Override 
                protected void onPreExecute(){
                    thoughtsDynamicListView = (ListView) findViewById(R.id.lvThoughts);                     

                    postParameters = new ArrayList<NameValuePair>();

                    dialog1 = new ProgressDialog(Thoughts.this, ProgressDialog.STYLE_SPINNER);
                    dialog1.setMessage("Loading Data...");          
                    dialog1.setCancelable(false);
                    dialog1.setCanceledOnTouchOutside(false);   

                    dialog1.show();
                }

                @Override
                protected String doInBackground(String... arg0) {
                    try {
                        response1 = CustomHttpClient.executeHttpPost("http://whatstherex.info/getT.php", postParameters);

                        res1 = response1.toString();

                        res1 = res1.replaceAll("null", "");

                        thoughtsList = res1.toString();

                        return thoughtsList;
                    }catch(Exception e){
                        res1 = e.toString();
                        return res1;
                    }
                }

                @Override
                protected void onPostExecute(String result1) {
                    dialog1.dismiss();

                    feed = result1.split("[_]");

                    task2 = new AsyncTask<String, Void, Drawable[]>() {         
                        ProgressDialog dialog2;
                        InputStream is;
                        Drawable[] d;
                        Drawable p4, p8, p12, p16, p20, p24, p28, p32, p36, p40, p44, p48, p52, p56, p60, p64, p68, p72, p76, p80, p84, p88, p92, p96, p100;
                        @Override 
                        protected void onPreExecute(){
                            dialog2 = new ProgressDialog(Thoughts.this, ProgressDialog.STYLE_SPINNER);
                            dialog2.setMessage("Loading Data...");          
                            dialog2.setCancelable(false);
                            dialog2.setCanceledOnTouchOutside(false);   

                            dialog2.show();
                        }

                        @Override
                        protected Drawable[] doInBackground(String... src) {
                            for(int i=0; i<src.length; i++){
                                try
                                {
                                    is = (InputStream) new URL(src[i]).getContent();
                                    d[i] = Drawable.createFromStream(is, "src name"+i);
                                }catch (Exception e) {
                                    e.toString();
                                }
                            }
                            return d;
                        }

                        @Override
                        protected void onPostExecute(Drawable[] result2) {
                            dialog2.dismiss();  

                            p4 = result2[1];
                            p8 = result2[2];
                            p12 = result2[3];
                            p16 = result2[4];
                            p20 = result2[5];
                            p24 = result2[6];
                            p28 = result2[7];
                            p32 = result2[8];
                            p36 = result2[9];
                            p40 = result2[10];
                            p44 = result2[11];
                            p48 = result2[12];
                            p52 = result2[13];
                            p56 = result2[14];
                            p60 = result2[15];
                            p64 = result2[16];
                            p68 = result2[17];
                            p72 = result2[18];
                            p76 = result2[19];
                            p80 = result2[20];
                            p84 = result2[21];
                            p88 = result2[22];
                            p92 = result2[23];
                            p96 = result2[24];
                            p100 = result2[25];

                            thoughtsInputs.add(new ThoughtsData(feed[1].toString(), feed[2].toString(), feed[3].toString(), p4));
                            thoughtsInputs.add(new ThoughtsData(feed[5].toString(), feed[6].toString(), feed[7].toString(), p8));
                            thoughtsInputs.add(new ThoughtsData(feed[9].toString(), feed[10].toString(), feed[11].toString(), p12));
                            thoughtsInputs.add(new ThoughtsData(feed[13].toString(), feed[14].toString(), feed[15].toString(), p16));
                            thoughtsInputs.add(new ThoughtsData(feed[17].toString(), feed[18].toString(), feed[19].toString(), p20));
                            thoughtsInputs.add(new ThoughtsData(feed[21].toString(), feed[22].toString(), feed[23].toString(), p24));
                            thoughtsInputs.add(new ThoughtsData(feed[25].toString(), feed[26].toString(), feed[27].toString(), p28));
                            thoughtsInputs.add(new ThoughtsData(feed[29].toString(), feed[30].toString(), feed[31].toString(), p32));
                            thoughtsInputs.add(new ThoughtsData(feed[33].toString(), feed[34].toString(), feed[35].toString(), p36));
                            thoughtsInputs.add(new ThoughtsData(feed[37].toString(), feed[38].toString(), feed[39].toString(), p40));
                            thoughtsInputs.add(new ThoughtsData(feed[41].toString(), feed[42].toString(), feed[43].toString(), p44));
                            thoughtsInputs.add(new ThoughtsData(feed[45].toString(), feed[46].toString(), feed[47].toString(), p48));
                            thoughtsInputs.add(new ThoughtsData(feed[49].toString(), feed[50].toString(), feed[51].toString(), p52));
                            thoughtsInputs.add(new ThoughtsData(feed[53].toString(), feed[54].toString(), feed[55].toString(), p56));
                            thoughtsInputs.add(new ThoughtsData(feed[57].toString(), feed[58].toString(), feed[59].toString(), p60));
                            thoughtsInputs.add(new ThoughtsData(feed[61].toString(), feed[62].toString(), feed[63].toString(), p64));
                            thoughtsInputs.add(new ThoughtsData(feed[65].toString(), feed[66].toString(), feed[67].toString(), p68));
                            thoughtsInputs.add(new ThoughtsData(feed[69].toString(), feed[70].toString(), feed[71].toString(), p72));
                            thoughtsInputs.add(new ThoughtsData(feed[73].toString(), feed[74].toString(), feed[75].toString(), p76));
                            thoughtsInputs.add(new ThoughtsData(feed[77].toString(), feed[78].toString(), feed[79].toString(), p80));
                            thoughtsInputs.add(new ThoughtsData(feed[81].toString(), feed[82].toString(), feed[83].toString(), p84));
                            thoughtsInputs.add(new ThoughtsData(feed[85].toString(), feed[86].toString(), feed[87].toString(), p88));
                            thoughtsInputs.add(new ThoughtsData(feed[89].toString(), feed[90].toString(), feed[91].toString(), p92));
                            thoughtsInputs.add(new ThoughtsData(feed[93].toString(), feed[94].toString(), feed[95].toString(), p96));
                            thoughtsInputs.add(new ThoughtsData(feed[97].toString(), feed[98].toString(), feed[99].toString(), p100));                  

                            adaptor = new ThoughtsCustomFeedAdaptor(Thoughts.this, R.layout.thoughtsdata, thoughtsInputs);
                            thoughtsDynamicListView.setAdapter(adaptor);

                        }
                    };                      
                    task2.execute(feed[4], feed[8], feed[12], feed[16], feed[20], feed[24], feed[28], feed[32], feed[36], feed[40], feed[44], feed[48], 
                            feed[52], feed[56], feed[60], feed[64], feed[68], feed[72], feed[76], feed[80], feed[84], feed[88], feed[92], feed[96], feed[100]);                                     
                }
            };
            task1.execute();
        }
    };
    task.execute();
}

1 个答案:

答案 0 :(得分:0)

我认为不可能像这样链接asynctask,因为asynctask应该从UI线程启动。

你应该在你的活动中做一个处理程序(在UI线程中执行)

mhandler = new Handler(getMainLooper(),new Callback() {

            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                case (1):
                    task1.execute();
                    break;
                case(2):    
                    break;
                    task2.execute();

                return false;
            }
        });

在你的asyncTask中添加类似的东西,而不是直接启动任务

Message m = new Message();
m.what = 1; // 1 or 2 depends of your task
mhandler.sendMessage(m);

使用此处理程序,您的任务将从UI线程开始。