Search code examples
androidcalllog

Android - using progressdialog with listview


I am collecting data from the calllog, putting it into a database, and querying them into a ListView. With 500 calls it takes around 8 seconds, which looks bad, so I decided to implement a progressdialog to show the user how many items are left to be loaded. Now I am trying with 3 items (there are 3 items in the calllog of the emulator). When the progressdialog finishes, the listview is populated with the 3 items, but then the same 3 items are added again and again making an endless loop.

I empty the table before populating it, so it has always three records.

What can be the problem?

pd = new ProgressDialog(Calllogs.this);
        pd.setCancelable(true);
        pd.setMessage("Loading...");
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setProgress(0);
        pd.setMax(3);
        pd.show();


        //incrementProgressBy
        new Thread(new Runnable() {                 
            @Override
            public void run() {
                   try
                   {
                    while(pd.getProgress()<=pd.getMax())
                      {
                        Thread.sleep(1000);

//collect data and populate database table
//query data and populate arrays

 handler.sendMessage(handler.obtainMessage());
        if(pd.getProgress()==pd.getMax())
        {
             pd.dismiss();
             adapter = new ListViewCustomAdapter(Calllogs.this, arr_calllog_name, arr_calllog_phone, arr_calllog_type,arr_calllog_duration, arr_calllog_date);
             lv1.post(new Runnable() {
                 public void run() {
                     lv1.setAdapter(adapter);
               }}); 

        }
        }
    }catch(Exception e){}
   }
   }).start();

And the handler:

 private Handler handler = new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                             super.handleMessage(msg);
                            pd.incrementProgressBy(1);    
                            //pd.dismiss();


                        }
                };

EDIT: i accidentally found out that the whole table population has become an endless loop. I checked the table with sqlite database browser and at first it had 21 items, a couple of seconds after 59, and growing and force close at last.

SOLUTION:

I can't believe i could not see this. The thread started with the population and that casued the endless loop. I had to put it after the calllog query and before the database query.


Solution

  • I can't believe i could not see this. The thread started with the population and that casued the endless loop. I had to put it after the calllog query and before the database query.