halilkaya
halilkaya

Reputation: 467

Android getting packageName from OnItemClickListener

I put application labels into ListView. I want to get packageName (com.blabla.blabla) from OnItemClickListener. How can I do it? My code here:

uygList = new ArrayList<String>();

final PackageManager pm = getPackageManager();
List<ApplicationInfo> paketler = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo paketBilgi : paketler) {

    if (paketBilgi.packageName.equals(c.getString(0))) {

        uygList.add(paketBilgi.loadLabel(pm).toString());

        if (sayi<c.getCount()-1) {
            sayi++;
            c.moveToNext();
        }

    }

}

lv.setAdapter(new ArrayAdapter<String>(Uygulamalar.this, android.R.layout.simple_list_item_1, uygList));

lv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {

        // I want to get the packageName that selected from ListView like (com.blabla.blabla)

    }
});

Upvotes: 0

Views: 308

Answers (2)

yichouangle
yichouangle

Reputation: 61

public void onItemClick(AdapterView parent, View view, int pos, long id) {

    // I want to get the packageName that selected from ListView like (com.blabla.blabla)

}

param pos is the positon in uygList

Upvotes: -1

gladmax
gladmax

Reputation: 347

You can use this:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
            for (ApplicationInfo paketBilgi : paketler) {
                String s = paketBilgi.loadLabel(pm).toString();
                if(s.equals(uygList.get(pos))){
                    String packageName = paketBilgi.packageName;
                }
            }
        }
    });

But if 2 apps have same label there will be problem so it would be better if you can use   another list and store the package names and use it.

final List<String> packageNames = new Arraylist<String>();

uygList.add(paketBilgi.loadLabel(pm).toString()); 
packageNames.add(paketBilgi.packageName);

lv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        String packageName =  packageNames.get(pos);
    // I want to get the packageName that selected from ListView like (com.blabla.blabla)
    }
});

Upvotes: 2

Related Questions