【问题标题】:Unable to show progress bar before displaying data into listview inside Dialog在将数据显示到对话框内的列表视图之前无法显示进度条
【发布时间】: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


【解决方案1】:

根据我从您的问题描述中了解到的情况,您正在尝试显示 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

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多