Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display progressdialog while listview is loading

Tags:

android

I want to display ProgressDialog while listview is loading. I've already tried like following coding but it didn't work.

final ProgressDialog dialog = ProgressDialog.show(this, "indeterminate", "Please wait while loading", true);
final Handler handler = new Handler() {
   public void handleMessage(Message msg) {
      dialog.dismiss();
      }
   };
Thread checkUpdate = new Thread() {  
   public void run() {
      // listview will be populated here    
      PopulateListView();           
      handler.sendEmptyMessage(0);
      }
   };
checkUpdate.start();

But it didn't work at all.

like image 614
PPShein Avatar asked Aug 15 '11 02:08

PPShein


2 Answers

i have run into the same a long time ago, but got it solved, sorry that the code is not commented , but any doubts i will be glad to help you.

This is the code for a filling a ListView with a whatever from a DB using a custom adapter an a ProgressDialog while loading

public class whateverListActivity extends ListActivity 
{

    private ProgressDialog mProgressDialog = null;
    private ArrayList<Whatever> mWhatevers = null;
    private WhateverAdapter mAdapter;
    private Runnable mViewWhatevers;
    private SQLiteDatabase mDatabase;
    private WhateverHelper mWhateverHelper;

    /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) 
     {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.whatever_list);
         mWhatevers = new ArrayList<Whatever>();
         mAdapter = new WhateverAdapter(this, R.layout.whatever_row, mWhatevers);
         setListAdapter(this.mAdapter);
         SQLiteOpenDataHelper helper = new SQLiteOpenDataHelper(this); 
         mDatabase = helper.getWritableDatabase();
       }



     @Override
    protected void onResume() {
            try{
         super.onResume();
        mWhateverHelper = new WhateverHelper(this,mDatabase);
        startGetWhatevers();
            } catch (Exception e) {
                 Log.e("BACKGROUND_PROC", e.getMessage());
               }
    }

     private void startGetWhatevers(){
          mViewWhatevers = new Runnable(){
              public void run() {
                  getWhatevers();
              }
          };
          Thread thread =  new Thread(null, mViewWhatevers, "Background");
          thread.start();
          mProgressDialog = ProgressDialog.show(this,    
                "Please wait...", "Retrieving data ...", true);
     }
     private void getWhatevers(){
        try{
            mWhatevers = new ArrayList<Whatever>();
            for (Whatever whatever : mWhateverHelper.getCurrentWhatevers()) {
                mWhatevers.add(whatever);
            }
            Thread.sleep(1500);
            Log.i("ARRAY", ""+ mWhatevers.size());
          } catch (Exception e) {
            Log.e("BACKGROUND_PROC", e.getMessage());
          }
          runOnUiThread(returnRes);
      }

    private Runnable returnRes = new Runnable() {
        public void run() {
            mAdapter.clear();
            if(mWhatevers != null && mWhatevers.size() > 0){
                mAdapter.notifyDataSetChanged();
                for(int i=0;i<mWhatevers.size();i++)
                mAdapter.add(mWhatevers.get(i));
            }
            mProgressDialog.dismiss();
            mAdapter.notifyDataSetChanged();
        }
      };
}

Hope this helps. Alex.

like image 56
Ale K. Avatar answered Nov 13 '22 04:11

Ale K.


ppshein... I think you are missing a few calls.

1) new Thread( new Runnable() {
2) super.handleMessage
3) switch(msg.what)

I have working code here. Also AsyncTask here.

like image 23
JAL Avatar answered Nov 13 '22 04:11

JAL