加载listview时显示progressdialog

时间:2011-08-15 02:48:40

标签: android

我想在listview加载时显示ProgressDialog。我已经尝试过如下编码,但它没有用。

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();

但它根本不起作用。

2 个答案:

答案 0 :(得分:4)

很久以前我遇到过同样的事情,但是得到了解决,对不起代码没有评论,但是我很乐意帮助你。

这是填充ListView的代码,其中包含使用自定义适配器和加载时ProgressDialog的数据库中的任何内容

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();
        }
      };
}

希望这会有所帮助。 亚历克斯。

答案 1 :(得分:1)

ppshein ......我想你错过了一些电话。

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

我有工作代码here. AsyncTask here.