这是你需要知道的。看看这个。您可以查看所有 Android 的默认布局。您正在访问android.R.layout.simple_list_item_1
的包含一个TextView
. 所以显然图标不会显示。看看源代码。
因此,要获得您想要的内容,您需要使用自定义适配器并将其设置到您的列表中。这是我的一个应用程序中的一个示例。它列出了icon
, application name
, package name
。另外,我建议您使用 anAsyncTask
来生成列表。
行.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="wrap_content"
android:layout_margin="3dp"
android:padding="6dip" >
<ImageView
android:id="@+id/iconView"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/appName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/iconView"
android:layout_toRightOf="@+id/iconView"
android:layout_marginLeft="3dp"
android:text="Application name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/packageName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/appName"
android:layout_below="@+id/appName"
android:text="Package name"
android:textSize="12sp"
android:layout_marginLeft="3dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/versionName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/packageName"
android:layout_below="@+id/packageName"
android:textSize="12sp"
android:text="Version Name"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/versionCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/packageName"
android:layout_toRightOf="@+id/versionName"
android:textSize="12sp"
android:text="Version Code"
android:layout_marginLeft="3dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
activity_app_list.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AppList" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
应用列表.java
package com.torcellite.de_addiction;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class AppList extends Activity {
private static ListView lView;
private static String appnames[], pnames[], versionnames[];
private static int versionCodes[];
private static Drawable icons[];
private static AppAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_list);
lView = (ListView) findViewById(R.id.listView1);
new loadApps().execute();
}
class loadApps extends AsyncTask<Void, Void, Void>
{
private ProgressDialog pd;
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
getPackages();
return null;
}
@Override
protected void onPreExecute()
{
pd = new ProgressDialog(AppList.this);
pd.setIndeterminate(true);
pd.setCancelable(false);
pd.setMessage("Loading applications..");
pd.show();
}
@Override
protected void onPostExecute(Void result)
{
pd.dismiss();
lView.setAdapter(adapter);
}
}
class PInfo {
private String appname = "";
private String pname = "";
private String versionName = "";
private int versionCode = 0;
private Drawable icon;
private void addToList(int i) {
appnames[i]=appname;
pnames[i]=pname;
versionnames[i]=versionName;
versionCodes[i]=versionCode;
icons[i]=icon;
}
}
private ArrayList<PInfo> getPackages() {
ArrayList<PInfo> apps = getInstalledApps(false); /* false = no system packages */
final int max = apps.size();
appnames = new String[max];
pnames = new String[max];
versionnames = new String[max];
versionCodes = new int[max];
icons = new Drawable[max];
for (int i=0; i<max; i++) {
apps.get(i).addToList(i);
}
adapter=new AppAdapter(AppList.this, R.layout.row, appnames, pnames, versionnames, versionCodes, icons);
return apps;
}
private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
ArrayList<PInfo> res = new ArrayList<PInfo>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
List<ApplicationInfo> appli = getPackageManager().getInstalledApplications(0);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
if ((appli.get(i).flags & ApplicationInfo.FLAG_SYSTEM)!=0&&(!getSysPackages) || (p.versionName == null)) {
continue ;
}
PInfo newInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(newInfo);
}
return res;
}
public static class AppAdapter extends BaseAdapter {
private static LayoutInflater mInflater;
// Keep all Images in array
private static Drawable[] mThumbIds;
private static String appnames[], Pnames[], versionnames[];
private static int versionCodes[];
private static int mViewResourceId;
private static ImageView pic;
private static TextView tv1, tv2, tv3, tv4;
// Constructor
public AppAdapter(Context ctx, int viewResourceId,String[] names, String[] pnames, String[] vnames, int[] vcodes, Drawable[] icons) {
mInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mThumbIds = icons;
appnames = names;
Pnames = pnames;
versionnames = vnames;
versionCodes = vcodes;
mViewResourceId = viewResourceId;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, lView, false);
pic = (ImageView) convertView.findViewById(R.id.iconView);
tv1 = (TextView) convertView.findViewById(R.id.appName);
tv2 = (TextView) convertView.findViewById(R.id.packageName);
tv3 = (TextView) convertView.findViewById(R.id.versionName);
tv4 = (TextView) convertView.findViewById(R.id.versionCode);
//Drawable background = new BitmapDrawable(mThumbIds[position]);
pic.setImageDrawable(mThumbIds[position]);
pic.invalidate();
tv1.setText(appnames[position]);
tv2.setText(Pnames[position]);
tv3.setText(versionnames[position]);
tv4.setText(versionCodes[position]+"");
return convertView;
}
}
}