我已经安装了所有应用程序,并且拥有一个列表视图。我需要onItemLongClick卸载我在列表视图中单击的应用程序。 onItemLongClick的起始代码是这样的:

@Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        return false;
    }


这是用于卸载:

ApplicationInfo app = applist.get(position);

        Uri packageUri = Uri.parse("package:"+app.packageName);
        Intent uninstallIntent =
          new Intent(Intent.ACTION_DELETE, packageUri);
        startActivity(uninstallIntent);

        return true;


我还需要插入一些参数,并尝试使用此参数,但是在onItemLongClick中出现错误:

protected boolean setOnItemLongClickListener(ListView l, View v, int position, long id) {
        super.onItemLongClick(l, v, position, id);// Error

        ApplicationInfo app = applist.get(position);

        Uri packageUri = Uri.parse("package:"+app.packageName);
        Intent uninstallIntent =
          new Intent(Intent.ACTION_DELETE, packageUri);
        startActivity(uninstallIntent);

        return true;
    }


我该如何解决?

最佳答案

尝试实现这个

import android.widget.AdapterView.OnItemLongClickListener;


yourListView.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "delete item in position : " + arg2, Toast.LENGTH_SHORT).show();
                return false;
            }
        });

关于java - Listview和onItemLongClick不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19618446/

10-10 20:07