-1

我正在尝试列出用户安装的应用程序及其图标。到目前为止,我设法让用户安装了应用程序,但我无法将它与应用程序名称和图标一起放在列表中。

例如列表的行:: {icon} test_application

活动

ArrayList<String> listing = null;
listing = new ArrayList<String>();
ArrayList<Object> icons = new ArrayList<Object>();
CharSequence c = null;
int count = 0;
Drawable icon = null;

super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView i = (TextView) findViewById(R.id.textView1);

    PackageManager pm = getPackageManager();
    List<ApplicationInfo> packages = pm
            .getInstalledApplications(PackageManager.GET_META_DATA);


    for (ApplicationInfo applicationInfo : packages) {
        if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 1) {

            count = count + 1;
            try {
                c = pm.getApplicationLabel(pm.getApplicationInfo(
                        applicationInfo.processName,
                        PackageManager.GET_META_DATA));
                icon = pm.getApplicationIcon(applicationInfo.packageName);

            } catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            listing.add(c.toString());
                            icons.add(icon)
        }
    }

    ListView list = (ListView) findViewById(R.id.listView1);
    list.setAdapter(new ArrayAdapter<String>(this, R.layout.text, R.id.textView1, listing));
    String Counting = String.valueOf(count);
    i.setText(Counting);
}

}

xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        tools:context=".MainActivity" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >

    </ListView>

</RelativeLayout>

行 xml

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/imageView1"
        />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_toRightOf="@+id/textView1"
        android:text="TextView" />

</RelativeLayout>
4

1 回答 1

0

链接具有使用文本和图像创建列表的示例代码。

您还需要将图标存储在另一个数据结构中(可能是另一个数组列表)。然后,您必须创建自己的适配器来创建需要视图。查看帖子以获取示例代码..

以下是您需要遵循的步骤:

1)创建一个类,如下所示:

class MyAppInfo{
public String  appName;
public Drawable icon;
}

2)现在更改数组列表以获取此对象,如下所示:

ArrayList<MyAppInfo> listing = null;
listing = new ArrayList<MyAppInfo>();

3) 现在在您的循环中创建 MyAppInfo 对象并将应用程序的名称和图标存储在该对象中,并将该对象添加到 arraylist 中,如下所示。

 PackageManager pm = getPackageManager();
    List<ApplicationInfo> packages = pm
            .getInstalledApplications(PackageManager.GET_META_DATA);
    MyAppInfo tempObj;

    for (ApplicationInfo applicationInfo : packages) {
        if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 1) {
                tempObj = new MyAppInfo();

            count = count + 1;
            try {
                c = pm.getApplicationLabel(pm.getApplicationInfo(
                        applicationInfo.processName,
                        PackageManager.GET_META_DATA));
                icon = pm.getApplicationIcon(applicationInfo.packageName);

                 tempObj.appName = c.toString();
                 tempObj.icon= icon;

            } catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            listing.add(tempObj);

        }
    }

4)现在创建您自己的适配器并在获取视图中使用此列表来创建ImageView图标和TextView应用程序名称......

于 2012-11-21T07:47:12.350 回答