0

我有一个包含按钮的活动,当您按下按钮时,它会加载第二个活动。第二个活动有一个 listView 和一堆其他东西,在大多数设备上加载需要几秒钟,所以我想显示一个 progressDialog 让用户知道活动实际上正在加载。我有以下内容:

编辑:

public void rewardFirstNext(View view){

    AsyncTask<Void, Void, Void> progress = new AsyncTask<Void, Void, Void>() {
        private ProgressDialog progressDialog;

        protected void onPreExecute(){
            progressDialog = new ProgressDialog(getApplicationContext());
            progressDialog.setTitle("Loading Installed Applications");
            progressDialog.setMessage("Please wait.");
            progressDialog.setCancelable(false);
            progressDialog.setIndeterminate(true);
            progressDialog.show();
        }

        protected Void doInBackground(Void... voids) {
            PackageManager mPackageManager = getPackageManager();
            List<Applications> packageInfoListApps = new ArrayList<Applications>();
            List<PackageInfo> packageInfoList = mPackageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS);
            List<PackageInfo> installedApps = new ArrayList<PackageInfo>();

            for (PackageInfo apps : packageInfoList) {
                if (!isSystemPackage(apps)) {
                    installedApps.add(apps);
                }

            }
            Collections.sort(installedApps, new Comparator<PackageInfo>() {
                public int compare(PackageInfo o1, PackageInfo o2) {
                    return o1.applicationInfo.loadLabel(getPackageManager()).toString().compareToIgnoreCase(o2.applicationInfo.loadLabel(getPackageManager()).toString());
                }
            });
            for (PackageInfo apps : installedApps) {
                packageInfoListApps.add(new Applications(apps, false, false));
            }

            //what else happens here?
            return null;
        }

        protected void onPostExecute(Void result){
            progressDialog.dismiss();
        }
    };

    progress.execute((Void[]) null);
}
4

1 回答 1

1

你应该做的就是把所有这些

mPackageManager = getPackageManager();
List<Applications> packageInfoListApps = new ArrayList<Applications>();
List<PackageInfo> packageInfoList = mPackageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS);
List<PackageInfo> installedApps = new ArrayList<PackageInfo>();

for (PackageInfo apps : packageInfoList) {
    if (!isSystemPackage(apps)) {
        installedApps.add(apps);
    }

}
Collections.sort(installedApps, new Comparator<PackageInfo>() {
    public int compare(PackageInfo o1, PackageInfo o2) {
        return o1.applicationInfo.loadLabel(getPackageManager()).toString().compareToIgnoreCase(o2.applicationInfo.loadLabel(getPackageManager()).toString());
    }
});
for (PackageInfo apps : installedApps) {
    packageInfoListApps.add(new Applications(apps, false, false));
}

在异步任务内部。

拿这个new ApplicationAdapter(this, packageInfoListApps, mPackageManager)并保留它的参考,如

ApplicationAdapter adater = new ApplicationAdapter(this, packageInfoListApps, mPackageManager)

然后像您一样在 onPostCreate 调用中启动您的进度对话框

adapter.notifyDataSetChanged();

然后它将填充列表视图,然后您也应该停止对话框

于 2013-07-17T16:30:20.900 回答