listview期间的Android对话栏

时间:2014-06-14 12:45:19

标签: android progressdialog

我正在尝试将项目添加到列表中,我想在循环开始之前启动对话框,在循环结束后将项目添加到列表并关闭对话框

     ProgressDialog pd;
     pd = new ProgressDialog(SearchList.this);
       pd.setTitle("Please wait");
       pd.setMessage("loading String.. ");
       pd.show();

    new Thread(new Runnable() {
    @Override
        public void run() {
        try {

                for (int i = 0; i <500000; i++) {
         list.add("String "+i);
         }
                pd.dismiss();
            } catch (Exception e) {

            }

        }
    }).start();

        m_adapter = new Adapter(this, R.layout.itemview, list);
    lv = (ListView) findViewById(R.id.listView1);
    lv.setAdapter(m_adapter);

3 个答案:

答案 0 :(得分:0)

使用AsyncTask显示/隐藏ProgressDialog,如下所示:

protected void onPreExecute() {
    dialog=ProgressDialog.show(mContext, "", "Your message");
    super.onPreExecute();
}

protected void onPostExecute(Void result) {
    if(dialog!=null)
    {
        dialog.dismiss();
    }
}

使用doInBackground函数进行循环:

protected Void doInBackground(String... params) {}

See example how to use AsyncTask

答案 1 :(得分:0)

Try this way,hope this will help you to solve your problem.

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

          getList(this,new AddItemFinsihListener() {
            @Override
            public void onFinish(Object object) {
              ArrayList<String> list = (ArrayList<String>) object;
              for (String row : list){
                 System.out.println(row);    
              }
             // try to set your list adapter here
            }
          });
        }

        public void getList(final Context context, final AddItemFinsihListener target) {
        final ProgressDialog  pd = new ProgressDialog(context);
        new AsyncTask<Void,Void,Object>(){
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pd.setTitle("Please wait");
                pd.setMessage("Page is loading..");
                pd.show();
            }

            @Override
            protected Object doInBackground(Void... params) {
                ArrayList<String> list = new ArrayList<String>();
                for (int i = 0; i <500000; i++) {
                    list.add("String "+i);
                }
                return list;
            }

            @Override
            protected void onPostExecute(Object object) {
                super.onPostExecute(object);
                pd.dismiss();
                target.onFinish(object);
            }
        }.execute();

       }

       interface AddItemFinsihListener{
           public void onFinish(Object object);
       }

答案 2 :(得分:0)

在初始化适配器并将其添加到ListView之前,需要先添加Array。

试试这个:

       ProgressDialog pd;
       pd = new ProgressDialog(SearchList.this);
       pd.setTitle("Please wait");
       pd.setMessage("loading String.. ");
       pd.show();

    new Thread(new Runnable() {
    @Override
        public void run() {
        try {

                for (int i = 0; i <500000; i++) {
                 list.add("String "+i);
                }
                SearchList.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() { 
                         m_adapter = new Adapter(this, R.layout.itemview, list);
                         lv = (ListView) findViewById(R.id.listView1);
                         lv.setAdapter(m_adapter);
                         pd.dismiss();
                    }
                });

            } catch (Exception e) {

            }

        }
    }).start();