-1

i am trying to list down the applications installed by the user and its icons. so far i managed to get the user installed applications but i am having trouble putting it in the list with the application name and icon together.

Eg row of the list:: {icon} test_application

activity

ArrayList<String> listing = null;
listing = new ArrayList<String>();
ArrayList<Object> icons = new ArrayList<Object>();
CharSequence c = null;
int count = 0;
Drawable icon = null;

super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView i = (TextView) findViewById(R.id.textView1);

    PackageManager pm = getPackageManager();
    List<ApplicationInfo> packages = pm
            .getInstalledApplications(PackageManager.GET_META_DATA);


    for (ApplicationInfo applicationInfo : packages) {
        if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 1) {

            count = count + 1;
            try {
                c = pm.getApplicationLabel(pm.getApplicationInfo(
                        applicationInfo.processName,
                        PackageManager.GET_META_DATA));
                icon = pm.getApplicationIcon(applicationInfo.packageName);

            } catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            listing.add(c.toString());
                            icons.add(icon)
        }
    }

    ListView list = (ListView) findViewById(R.id.listView1);
    list.setAdapter(new ArrayAdapter<String>(this, R.layout.text, R.id.textView1, listing));
    String Counting = String.valueOf(count);
    i.setText(Counting);
}

}

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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        tools:context=".MainActivity" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >

    </ListView>

</RelativeLayout>

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="fill_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/imageView1"
        />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_toRightOf="@+id/textView1"
        android:text="TextView" />

</RelativeLayout>
Romi
  • 119
  • 2
  • 10

1 Answers1

0

This link has the sample code to create list with text and image.

you need to store the icon also in another data structure (may be one more array list). Then you would have to create your own adapter that create the require view. Check this post out for example code..

Following are the steps you need to follow:

1) Create one class like following:

class MyAppInfo{
public String  appName;
public Drawable icon;
}

2) Now change the array list to take this object like following:

ArrayList<MyAppInfo> listing = null;
listing = new ArrayList<MyAppInfo>();

3) Now in your loop create object of MyAppInfo and store the name of the app and icon in that object and add that object to arraylist like following.

 PackageManager pm = getPackageManager();
    List<ApplicationInfo> packages = pm
            .getInstalledApplications(PackageManager.GET_META_DATA);
    MyAppInfo tempObj;

    for (ApplicationInfo applicationInfo : packages) {
        if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 1) {
                tempObj = new MyAppInfo();

            count = count + 1;
            try {
                c = pm.getApplicationLabel(pm.getApplicationInfo(
                        applicationInfo.processName,
                        PackageManager.GET_META_DATA));
                icon = pm.getApplicationIcon(applicationInfo.packageName);

                 tempObj.appName = c.toString();
                 tempObj.icon= icon;

            } catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            listing.add(tempObj);

        }
    }

4) Now create your own adapter and use this list in get view to create ImageView for icon and TextView for app name...

Community
  • 1
  • 1
Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
  • yes Praful i still struggle with how to add this icon to the list. if u can put out the code for me, i would be really greatful – Romi Nov 21 '12 at 08:00
  • I would suggest you to start using the sample for your view and keep us updated on the new problem you face while using the code. So we can help on that.... – Praful Bhatnagar Nov 21 '12 at 08:02
  • yes for that i should know what i should do 1st... in what u gave it talks about Hashmap... but still does not gives me a solution. so but im trying out with Hashmaps, its not im not trying. i just want to improve my code thats all – Romi Nov 21 '12 at 08:05
  • first create one more array list with icons – Praful Bhatnagar Nov 21 '12 at 08:05
  • yes it helps correctly....... :) but want to know on the way i was doing, is it possible? if it is possible how to set the adapter to display the icon and text? – Romi Nov 21 '12 at 08:17
  • There are plenty of tutorial out there that can help you in creating a adapter for ListView.. i would suggest do a search in SO or Google and you would find may example with code.. – Praful Bhatnagar Nov 21 '12 at 08:30