gpt4 book ai didi

android - 在加载 ListView 时显示进度对话框

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:01:57 26 4
gpt4 key购买 nike

我想在加载 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();

但是根本没用。

最佳答案

我很久以前也遇到过同样的问题,但是解决了,很抱歉代码没有注释,但有任何疑问我很乐意帮助你。

这是在加载时使用自定义适配器和 ProgressDialog 从数据库中填充 ListView 的代码

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

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

关于android - 在加载 ListView 时显示进度对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7061281/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com