在Dialog中将数据显示到listview之前无法显示进度条

时间:2015-02-12 05:48:35

标签: android listview android-listview android-asynctask android-alertdialog

我编写了一个程序,我允许用户点击按钮然后在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();

        }
    }

}

1 个答案:

答案 0 :(得分:0)

根据我对您的问题描述的理解,您正在尝试显示ProgressDialog,同时正在下载另一个ListViewDialog的内容。

我想建议,您可以在ProgressDialog中显示Dialog,而不是尝试在另一个ProgressBar上显示Dialog。已下载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的{​​{1}}和GONE的{​​{1}}的可见性设置为ListView

我希望这会有所帮助。