[英]Unable to show progress bar before displaying data into listview inside Dialog
我已經編寫了一個程序,在該程序中,我允許用戶點擊按鈕,然后在Dialog中顯示listview,並且我也能夠將數據填充到ListView中。
我得到的是:當我單擊按鈕時,顯示對話框,而在對話框中,我使用的是ListView(使用JSON從服務器中獲取數據),它將數據填充到Listview中,但是需要10到15秒的時間,並且沒有顯示進度欄(我是已經使用AsyncTask)。
我正在嘗試做的事情 :我想顯示15秒鍾的進度對話框
public class MainActivity extends Activity {
Button buttonOpenDialog;
static final int CUSTOM_DIALOG_ID = 0;
ListView dialog_ListView;
Dialog dialog = null;
ArrayList<Main> arrayList;
MainAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayList = new ArrayList<Main>();
buttonOpenDialog = (Button)findViewById(R.id.opendialog);
buttonOpenDialog.setOnClickListener(new Button.OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View arg0) {
showDialog(CUSTOM_DIALOG_ID);
}});
}
@Override
protected Dialog onCreateDialog(int id) {
switch(id) {
case CUSTOM_DIALOG_ID:
dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialoglayout);
dialog.setTitle("Custom Dialog");
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setOnCancelListener(new OnCancelListener(){
@Override
public void onCancel(DialogInterface dialog) {
}});
dialog.setOnDismissListener(new OnDismissListener(){
@Override
public void onDismiss(DialogInterface dialog) {
}});
//Prepare ListView in dialog
dialog_ListView = (ListView)dialog.findViewById(R.id.dialoglist);
new JSONAsyncTask().execute(".....");
adapter = new MainAdapter(getApplicationContext(), R.layout.row, arrayList);
dialog_ListView.setAdapter(adapter);
dialog_ListView.setOnItemClickListener(new OnItemClickListener(){
@SuppressWarnings("deprecation")
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
dismissDialog(CUSTOM_DIALOG_ID);
}});
break;
}
return dialog;
}
@SuppressWarnings("deprecation")
@Override
protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
// TODO Auto-generated method stub
super.onPrepareDialog(id, dialog, bundle);
switch(id) {
case CUSTOM_DIALOG_ID:
break;
}
}
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(dialog.getContext());
progressDialog.setMessage("Loading, please wait");
progressDialog.show();
progressDialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("data");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Main main = new Main();
main.setTitle(object.getString("title"));
Log.v("title:", object.getString("title"));
arrayList.add(main);
}
return true;
}
//------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
progressDialog.cancel();
adapter.notifyDataSetChanged();
if(result == false)
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
}
據我對問題描述的了解,您正在嘗試顯示一個ProgressDialog
同時正在下載另一個Dialog
中的ListView
的內容。
我想建議您,而不是嘗試在另一個Dialog
上顯示ProgressDialog
,您可以僅在Dialog
顯示一個ProgressBar
,然后在下載ListView
的內容之后,可以顯示ListView
。
您將需要一個類似於Dialog
的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dialogListLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<ListView
android:id="@+id/dialoglist"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:visibility="gone"/>
</LinearLayout>
下載ListView
的內容后,應將ProgressBar
的可見性設置為GONE
,將ListView
的可見性設置為VISIBLE
。
我希望這有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.