所以,我有我的onCreate方法在这里填充用户安装的应用程序ListView
。这需要很长时间才能完成,我试图找出在哪里创建新线程来完成一些繁重的工作,以便在列表加载时显示ProgressDialog
。这里是我的代码有:Android:适当的线程化ListActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Find out which prefs to update
Bundle extras = getIntent().getExtras();
if(extras !=null){
buttonPressed = extras.getString("buttonPressed");
loadNumber = extras.getString("loads");
}
buttonPressedAppName = buttonPressed + "AppName" + loadNumber;
buttonPressedAppPack = buttonPressed + "AppPack" + loadNumber;
humanNamePrefs = buttonPressed + "AppText" + loadNumber;
// Get shared preferences
settings = getSharedPreferences(PREFS_NAME, 0);
// Do in another thread to not slow the UI down...eventually
adapter = createAdapter();
setListAdapter(adapter);
}
public ListAdapter createAdapter() {
namesArray = new String[] { "Loading" };
names = new ArrayList<String>();
PackageManager pm = this.getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
appList = pm.queryIntentActivities(mainIntent, 0);
Collections.sort(appList, new ResolveInfo.DisplayNameComparator(pm));
// Now, appList contains all of the ResolveInfo stuff
for (int i = 0; i < appList.size(); i++) {
names.add(appList.get(i).loadLabel(pm).toString());
}
namesArray = maker(names);
ListAdapter adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, namesArray);
setListAdapter(adapter);
return adapter;
}
我有一个线程处理列表和这样的排序,但ProgressDialog
我根本不会露面,直到列表完全填充,哪种击败目的在于拥有ProgressDialog
。
我的问题,简而言之,我应该把这个线程用于显示ProgressDialog
WHILE列表正在完成填充?
以下内容得到
@Femi上AsyncTask
提供了一个极好的教程和我的应用程序列表中的加载过程中有我ProgressDialog
捻转。谢谢!
链接:http://labs.makemachine.net/2010/05/android-asynctask-example/
你看过使用asynctask吗?在执行前显示进度,然后关闭onPostExecute – jkhouw1 2011-05-08 02:22:27
查看本教程以查看工作实施。 http://p-xr.com/android-tutorial-how-to-make-a-progress-dialog/ – 2011-05-08 05:15:24