ListView正在加载时无法显示Indeterminate ProgressBar

时间:2011-05-16 21:01:20

标签: android

我正在加载列表视图,其中列出了手机上所有已安装的应用。我希望应用程序在列表视图加载时显示一个不确定的进度条(旋转圆形)。

     final PackageManager pm = this.getPackageManager();

    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    final ArrayList<ResolveInfo> list =
            (ArrayList<ResolveInfo>) pm.queryIntentActivities(intent, 
                    PackageManager.PERMISSION_GRANTED);
    for (ResolveInfo rInfo : list)
    {
        Log.i(TAG, ": Installed Applications " + rInfo.activityInfo.
                applicationInfo.loadLabel(pm).toString());
    }

    final ArrayAdapter<ResolveInfo> adapter = 
        new ArrayAdapter<ResolveInfo>(this, R.layout.list_item, list)
        {
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            if (convertView == null)
                convertView = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.list_item, parent, false);

            final String text = list.get(position).activityInfo.
                applicationInfo.loadLabel(pm).toString();
            ((TextView)convertView.findViewById(R.id.text)).setText(text);

            final Drawable drawable = list.get(position).activityInfo.applicationInfo.loadIcon(pm);
            ((ImageView)convertView.findViewById(R.id.image)).setImageDrawable(drawable);

            return convertView;
        }

        };
    setListAdapter(adapter);

    ListView lv = getListView();
    lv.setEmptyView(findViewById(R.id.empty));
    lv.setTextFilterEnabled(true);

继承XML:

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView android:id="@+id/image" 
        android:layout_width="50dp" android:layout_height="50dp" />
    <TextView android:id="@+id/text"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:padding="10dp" android:textSize="16sp" />

 <ScrollView
            android:id="@+id/empty" android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" 
            android:layout_height="wrap_content"></ProgressBar>

        </ScrollView>

 </LinearLayout>

列表视图加载时没有显示......我做错了什么???

1 个答案:

答案 0 :(得分:2)

我认为您需要异步加载数据:

  1. 进行初始设置时,仅为活动设置主ListView并设置空视图
  2. 编写AsyncTask(链接 here ),以异步方式加载数据并在适配器完成时填充
  3. 初始设置后启动AsyncTask
  4. 在任务执行期间,应显示空视图(进度条),当任务完成时,应填充ListView