Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

android - display progressdialog while listview is loading

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...