我正在尝试列出用户安装的应用程序及其图标。到目前为止,我设法让用户安装了应用程序,但我无法将它与应用程序名称和图标一起放在列表中。
例如list :: {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>
答案 0 :(得分:0)
This链接包含用于创建包含文本和图像的列表的示例代码。
您还需要将图标存储在另一个数据结构中(可能是另一个数组列表)。然后,您必须创建自己的适配器来创建require视图。查看this发布的示例代码..
以下是您需要遵循的步骤:
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
...