【发布时间】:2015-02-12 05:48:34
【问题描述】:
我编写了一个程序,在该程序中,我允许用户点击按钮,然后在 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();
}
}
}
【问题讨论】:
-
显示
JSONAsyncTask类代码 -
@ρяσѕρєяK 我已经发布了 JSONAsyncTask 代码
-
@Sun
MainAdapter扩展了什么类?ArrayAdapter?BaseAdapter? -
@Sun:使用
ProgressDialog(dialog.getContext()); -
@ρяσѕρєяK 试过这个 progressDialog = new ProgressDialog(dialog.getContext());还没有完成!
标签: android listview android-listview android-asynctask android-alertdialog