0

我将应用程序标签放入 ListView。我想从 OnItemClickListener 获取packageName (com.blabla.blabla)。我该怎么做?我的代码在这里:

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)

    }
});
4

2 回答 2

2

你可以使用这个:

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)
    }
});
于 2013-02-24T11:17:13.933 回答
-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 是 uygList 中的位置

于 2013-02-24T11:49:00.860 回答