为什么图标没有显示,但图标名称正在列表中

时间:2013-05-19 13:04:58

标签: android android-listview

我有一个代码来显示我的设备中安装的所有应用程序...它工作正常但现在我只想显示应用程序的图标但是不显示图标,图标的名称写在列表中。像

android.graphics.drawable.BitmapDrawable@405369f0

怎么办??? 这是我的代码

private ArrayList<String> application_name = new ArrayList<String>();
private ArrayList<Drawable> application_icon = new ArrayList<Drawable>();
PackageInfo pInfo;
String packageName;
String app;
List<ResolveInfo> list;
TextView apple;
String myapps_name[]= new String[application_name.size()];
Drawable myapps_icon[]= new Drawable[application_icon.size()];
ResolveInfo rInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);

    //lView = (ListView) findViewById(R.id.list);
    PackageManager pm = this.getPackageManager();

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

    list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
    for (ResolveInfo rInfo: list)
    {
      application_name.add((String) rInfo.activityInfo.applicationInfo.loadLabel(pm));
      application_icon.add(rInfo.activityInfo.applicationInfo.loadIcon(pm));

      Log.w("Installed Applications", rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
    }
    getListView().setOnItemClickListener(this);

    myapps_name = application_name.toArray(myapps_name);
    myapps_icon=application_icon.toArray(myapps_icon);

    setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, myapps_icon));
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    //
    final Intent intent = new Intent(Intent.ACTION_MAIN);
    final ActivityInfo info = list.get(arg2).activityInfo;
    intent.setClassName(info.packageName, info.name);
    // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    startActivity(intent);
}

如果可能的话,我想同时显示每个已安装应用的图标和应用名称

2 个答案:

答案 0 :(得分:0)

实现你想要的东西,在你的情况下,ArrayAdapter并重写getView方法。 Infact是getView负责返回您要显示的视图。

答案 1 :(得分:0)

这是你需要知道的。检查此out。您可以查看Android的所有默认布局。您正在访问的android.R.layout.simple_list_item_1包含TextView。很明显,图标不会显示出来。看看source code

因此,要获得所需内容,您需要使用自定义适配器并将其设置为列表。这是我的一个应用程序的示例。它列出了iconapplication namepackage name。另外,我建议您使用AsyncTask生成列表。

row.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>

AppList.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;
    }
}

}