簡體   English   中英

在Asynctask非活動類的preExecute上無法打開進度對話框

[英]Progress dialog does not open on preExecute in Asynctask non-activity class

我正在使用kso​​ap調用Web服務。 當應用程序啟動時或當我滾動到頁面末尾時,應用程序正確地從Web服務中獲取數據,並按我的意願顯示數據,但是當我想在AsyncTask中顯示進度對話框時,它無法正常工作。 它顯示了PostExecute之后的進度對話框。 那么,我的代碼有什么問題呢? 我不明白我的問題在哪里。 ;-)

我有一個名為ElvatraWebService的Web服務類:

這是女巫使用Asynctasks獲取最新帖子的方法

private  String GetLastPublicPosts(int counter){
    ...
    ...
    ...
    ...
    return resTxt; //Returns JSON String 
}



public String getLatestPost(int counter, Activity a){

    CallLatestPost task = new CallLatestPost(counter,a);
    try {
        strJSON = task.execute("getLatest").get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //Log.i("sss", "--" + strJSON);
    return strJSON;

}

private class CallLatestPost extends AsyncTask<String, Void, String> {
    private int Counter = 0;
    private ProgressDialog dialog;
    private Activity activity;
    private Context context;


    public CallLatestPost (int c,Activity actt) {
        super();
        this.Counter = c;
        activity = actt;
        context = actt;
        dialog = new ProgressDialog(this.context);

    }

    @Override
    protected String doInBackground(String... params) {

        ElvatraWebService elws = new ElvatraWebService();
        String tmp = elws.GetLastPublicPosts(this.Counter);
        //Log.i("strJSON",strJSON);
        return tmp;
    }

    @Override
    protected void onPostExecute(String result) {
        //Set response 
        super.onPostExecute(result);

        if (dialog.isShowing()) {
            dialog.dismiss();
        }

    }

    @Override
    protected void onPreExecute() {

        super.onPreExecute();



        dialog = new ProgressDialog(context);
        dialog.setMessage("Connecting...");
        //dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        dialog.show();

        Log.i("@@@Async", "=== " + "PreExec");
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }

}

我有一個叫做崗位的班。 它將從Elvatrawebservice調用getLatestPost。 然后,它將使用PostAdapter類將JSON字符串轉換為JsonArray。 PostAdapter是BaseAdapter的擴展類。

在主要活動中,我在onCreate方法中調用一個稱為LoadContent的本地方法。 這是MainActivity類

 public class MainActivity extends Activity {


String displayText="";
public TextView tv;
public ListView lv;
public ProgressBar pg;


int theCounter=20;
String[] strTAG = new String[] {"title","photo","date","desc","linkurl"};
//int[] intField = new int[] {R.id.title,R.id.imgPost, R.id.Date, R.id.desc, R.id.link};
ListAdapter sa;
List<HashMap<String,Object>> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recentposts);


    lv = (ListView) findViewById(R.id.list);    
    lv.setOnScrollListener(new OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {

           final int lastItem = firstVisibleItem + visibleItemCount;
           if(lastItem == totalItemCount) {
               //load more data
            // Load content of listview
            LoadContent();


           }
        }
    });




}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}



@Override
public void onDestroy() {
    super.onDestroy();  // Always call the superclass

    // Stop method tracing that the activity started during onCreate()
    android.os.Debug.stopMethodTracing();
}



public void LoadContent(){

    lv = (ListView) findViewById(R.id.list);

    Post p = new Post(theCounter);
    theCounter+=20;
    if (p.GetLatestPosts(lv,MainActivity.this))
    {
        //tv.setText("true");

    }   

}   

}

你有

 task.execute("getLatest").get();

您不應該調用get()因為這會阻塞ui線程等待結果,從而使其不再異步任務異步。 只需使用

 task.execute("getLatest");

您可以在返回結果doInBackground和UI更新onPostExecute

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM