我想列出由用戶及其圖標安裝的應用程序。到目前爲止,我設法獲取用戶安裝的應用程序,但我無法將它與應用程序名稱和圖標放在一起。如何將應用程序前面的圖標添加到列表中
列表:: {}圖標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>
「但我無法把它在列表中的」準確你有什麼問題? – Simon
是什麼問題?你沒有得到列表視圖? –
你只有一個圖標變量。你打算如何在列表中使用它?你的代碼只會顯示應用程序的名稱,而不是圖標... –