2011-01-26 53 views
1

我有一個alertdialog,其中的數據來自互聯網。有時由於互聯網連接數據來遲。所以我想在Spinner展示自己之前進度條。同時獲取我寫的這些數據。如何在spinner之前設置進度條?

final String[] List= new String{ 'abc', 'def'}; 
       ArrayList<String> ListArray = new ArrayList<String>(); 
       ListArray.addAll(Arrays.asList(List)); 
       ListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, ListArray); 
       AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       builder.setTitle("Data"); 
       builder.setAdapter(ListAdapter, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int item) { 
         txtExec.setText(ClientList[item]); 
        } 
       }); 
       AlertDialog alert = builder.create(); 
       alert.show(); 


ProgressDialog dialog = new ProgressDialog(this); 
       dialog.setMessage("Please wait while loading..."); 
       dialog.setIndeterminate(true); 
       dialog.setCancelable(true); 

那麼,如何在提醒對話框之前調整progessDialog?

回答

1

您應該從一個線程或一個AsyncTask從互聯網加載數據。

here你有一篇小文章來描述可能性。

我推薦的AsyncTask方法,我張貼你如何做到這一點有點僞代碼,請參考文檔的關於如何通過你的doInBackground方法將onPostExecute例如加載數據的詳細信息。

private class MyTask extends AsyncTask<Void, Void, Void> { 

    protected void onPreExecute() { 
     //setup and display the progressDialog 
    } 

    protected Void doInBackground(Void... params) { 
     //do your data loading 
     return null; 
    } 

    protected void onPostExecute(Void void) { 
     //dismiss progress dialog and show the spinner 
    } 
} 
+0

thanxna很多.... – Sourabh 2011-04-19 07:48:04