0

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)

    }
});

2 Answers 2

2

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)
    }
});
0
-1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.