0

我正在尝试获取我的 android 设备中所有已安装应用程序的列表。这是我在启动器应用程序中的代码:

package com.powergroupbd.appfilter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ApplicationFilterActivity extends Activity {
    /** Called when the activity is first created. */

    ListView appfilter;
    // ArrayAdapter<Applications> adapter;
    PackageManager pck;
    private ArrayList<Applications> results = new ArrayList<Applications>();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        appfilter = (ListView) findViewById(R.id.lvApp);

        PackageManager packageManager = this.getPackageManager();
        List<PackageInfo> applist = packageManager.getInstalledPackages(0);
        Iterator<PackageInfo> it = applist.iterator();
        while (it.hasNext()) {
            PackageInfo pk = (PackageInfo) it.next();
            // if(PackageManager.PERMISSION_GRANTED==packageManager.checkPermission(Manifest.permission.ACCESS_FINE_LOCATION,
            // pk.packageName) &&
            // PackageManager.PERMISSION_GRANTED==packageManager.checkPermission(Manifest.permission.ACCESS_COARSE_LOCATION,
            // pk.packageName)) //checking if the package is having INTERNET
            // permission
            // {
            results.add(get(pk.applicationInfo.loadIcon(getPackageManager()), ""+pk.applicationInfo.loadLabel(packageManager)));
            Log.i("AppName", ""+pk.applicationInfo.loadLabel(packageManager));


            // }
        }

        // Collections.sort(results, new Comparator<Applications>() {
        // @Override
        // public int compare(Applications c1, Applications c2) {
        // return c1.getAppname().compareToIgnoreCase(c2.getAppname());
        // }
        // });

        appfilter.setAdapter(new ArrayAdapter<Applications>(this,
                android.R.layout.simple_list_item_1, results));
    }



    private Applications get(Drawable icon, String appname) {
        return new Applications(icon, appname);
    }
}

我的自定义适配器类在这里:

package com.powergroupbd.appfilter;

import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class Customarrayadapter extends ArrayAdapter<Applications> {

    private final List<Applications> list;
    private final Activity context;

    public Customarrayadapter(Activity context, List<Applications> list) {
        super(context, R.layout.row, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {

        protected ImageView icon;
        protected TextView appame;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.row, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.icon = (ImageView) view.findViewById(R.id.ivIcon);
            viewHolder.appame = (TextView) view.findViewById(R.id.tvAppName);
            viewHolder.appame.setTextColor(Color.WHITE);



            view.setTag(viewHolder);
        } else {
            view = convertView;

        }

        ViewHolder holder = (ViewHolder) view.getTag();
        holder.icon.setImageDrawable(list.get(position).getIcon());
        holder.appame.setText(list.get(position).getAppname());



                return view;
    }
}

这是我的应用程序类:

package com.powergroupbd.appfilter;

import android.graphics.drawable.Drawable;

public class Applications {

    private Drawable icon;
    private String appname;

    public Applications(Drawable icon, String appname) {

        this.icon = icon;
        this.appname = appname;

    }

    public Drawable getIcon() {
        return icon;
    }

    public void setIcon(Drawable icon) {
        this.icon = icon;
    }

    public String getAppname() {
        return appname;
    }

    public void setAppname(String appname) {
        this.appname = appname;
    }

}

最后清单是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.powergroupbd.appfilter"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ApplicationFilterActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

如果我运行该程序,那么它只显示一些垃圾字符串值列表(com.powergroupbd.appfilter.Applications@44297...),没有任何图标。

我该如何解决?

4

1 回答 1

2

您需要为 Applications 类覆盖它

因此,将其添加到您的Applications.java文件中:

public String toString(){
    return appname;
}

这将使 toString 调用返回应用程序实例的名称,而不是内存地址。您可以在此方法中将您喜欢的任何信息添加到返回的字符串中。

编辑:

改变这一行:

appfilter.setAdapter(new ArrayAdapter<Applications>(this, android.R.layout.simple_list_item_1, results));

对此:

appfilter.setAdapter(new Customarrayadapter(this, results)); //<-- also you should camelCase the name of your adapter class.
于 2012-06-04T18:44:31.337 回答