In my project, I wanted to list apps installed on my Android device.
But it should be custom list with icons.
For this purpose, I used the following code:
public class Main extends ListActivity { private String xmlResponse; private String error; static final ArrayList> list_arr = new ArrayList >(); private PackageManager pm; private String version; private void PopulateList () { Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent.CATEGORY_LAUNCHER); List list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED); list_arr.clear(); int i = 0; for (ResolveInfo rInfo : list) { String package1 = rInfo.activityInfo.applicationInfo.packageName; PackageInfo pInfo; version = ""; try { pInfo = getPackageManager().getPackageInfo(package1, PackageManager.GET_META_DATA); version = pInfo.versionName; } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String name1 = rInfo.activityInfo.applicationInfo.loadLabel(pm).toString(); Drawable icon = rInfo.activityInfo.applicationInfo.loadIcon(pm); i++; HashMap temp = new HashMap (); temp.put("name",name1); temp.put("value",package1); temp.put("value_bottom",version); temp.put("image", icon); list_arr.add(temp); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); pm = this.getPackageManager(); myAdapter adapter = new myAdapter( this, list_arr, R.layout.main, new String[] {"name","value","value_bottom","image"}, new int[] {R.id.option_name,R.id.option_value,R.id.option_value_bottom,R.id.icon} ); PopulateList(); setListAdapter(adapter); } private class myAdapter extends SimpleAdapter { public myAdapter(Context context, List extends Map > data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = getLayoutInflater().inflate(R.layout.main, null); } HashMap data = (HashMap ) getItem(position); ((TextView) convertView.findViewById(R.id.option_name)) .setText((String) data.get("name")); ((TextView) convertView.findViewById(R.id.option_value)) .setText((String) data.get("value")); ((TextView) convertView.findViewById(R.id.option_value_bottom)) .setText((String) data.get("value_bottom")); ((ImageView) convertView.findViewById(R.id.icon)) .setImageDrawable((Drawable) data.get("image")); return convertView; } }
main.xml file should look as following:
this seems not working. i have tried it and got no error but when i tried to run it, the application has been forced close.
could u please help me with it?
try to add in AndroidManifest.xml