0

i am trying to add items to list and i want to start dialog bar before the start of the loop which add items to list and dismiss dialog after loop finish

     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);
Ayman
  • 81
  • 2
  • 10

3 Answers3

0

Use AsyncTask to show/hide ProgressDialog as follows:

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

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

Use doInBackground function to do your looping:

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

See example how to use AsyncTask

Community
  • 1
  • 1
rizzz86
  • 3,862
  • 8
  • 35
  • 52
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);
       }
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

You need to add the Array first before you initialized the adapter and add it to the ListView.

try this:

       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();
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63