Android AlertDialog框没有显示,没有错误

时间:2015-01-31 04:22:53

标签: android alertdialog

更新:完全重写了问题以澄清

我正在创建一个AlertDialog来告诉用户保持(应用程序没有崩溃),同时应用程序提取应用程序列表,对其进行排序并将其放入列表视图中。如果我自己运行AlertDialog代码,它就可以运行。但结合其他代码,它什么也没做。没有错误,只是没有出现。应用程序列表在9个晚上就会出现。{/ p>

    public void onClick_ChooseApp(View v) {
        TextView desc = (TextView) v.findViewById(R.id.AppChoice_Desc);

        AlertDialog waitBox = new AlertDialog.Builder(this)
                .setView(getLayoutInflater().inflate(R.layout.wait_dialog, null))
                .show();

        final ArrayList<PackageInfo> pkgs = quickSort(new ArrayList(getPackageManager().getInstalledPackages(0)));
        for (int i=pkgs.size()-1; i>=0; i--) {
            Boolean criteria = (pkgs.get(i).applicationInfo.icon==0);
            if (criteria) {
                pkgs.remove(i);
            }
        }
        waitBox.dismiss();

        AlertDialog chooser = new AlertDialog.Builder(this)
                .setAdapter(new AppAdapter(this, pkgs), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String appName = pkgs.get(which).applicationInfo.loadLabel(getPackageManager()).toString();
                        Logg.d("selected " + which + "; " + appName);
                        chosenApp.setText(appName);
                        sharedPreferences.edit().putString("ChosenAppPkg", pkgs.get(which).packageName).apply();
                        sharedPreferences.edit().putString("ChosenAppName", appName).apply();
                    }
                })
                .show();
   }

wait_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ProgressBar
        android:indeterminate="true"
        android:id="@+id/marker_progress"
        style="@style/Base.Widget.AppCompat.ProgressBar"
        android:layout_height="50dp"
        android:layout_width="wrap_content"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="15dp"
        android:layout_gravity="center_vertical"
        android:gravity="center_horizontal"
        android:textSize="@dimen/abc_text_size_medium_material"
        android:text="Loading List of Apps"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

使用ASyncTask解决了这个问题。问题似乎是原始的AlertDialog在cpu密集型快速排序开始时还没有完成创建。 ASyncTask将它们分成不同的线程来解决问题。

 public void onClick_ChooseApp(View v) {
    final Context mThis = this;

    AsyncTask<Void, Void, ArrayList<PackageInfo>> task = new AsyncTask<Void, Void, ArrayList<PackageInfo>>() {

        @Override
        protected void onPreExecute() {
            pd = new ProgressDialog(mThis);
            pd.setMessage("Loading List of Apps");
            pd.setCancelable(false);
            pd.setIndeterminate(true);
            pd.show();
        }

        @Override
        protected ArrayList<PackageInfo> doInBackground(Void... arg0) {
            final ArrayList<PackageInfo> pkgs = quickSort(new ArrayList(getPackageManager().getInstalledPackages(0)));
            for (int i=pkgs.size()-1; i>=0; i--) {
                Boolean criteria = (pkgs.get(i).applicationInfo.icon==0);
                if (criteria) {
                    pkgs.remove(i);
                }
            }
            return pkgs;
        }

        @Override
        protected void onPostExecute(final ArrayList<PackageInfo> pkgs) {
            if (pd!=null) {
                pd.dismiss();
                AlertDialog chooser = new AlertDialog.Builder(mThis)
                        .setAdapter(new AppAdapter(mThis, pkgs), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String appName = pkgs.get(which).applicationInfo.loadLabel(getPackageManager()).toString();
                                Logg.d("selected " + which + "; " + appName);
                                chosenApp.setText(appName);
                                sharedPreferences.edit().putString("ChosenAppPkg", pkgs.get(which).packageName).apply();
                                sharedPreferences.edit().putString("ChosenAppName", appName).apply();
                            }
                        })
                        .show();
            }
        }

    };
    task.execute((Void[])null);

 }