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...) のリストが表示されます。
どうすれば修正できますか?