【问题标题】:Listview and onItemLongClick doesn't workListview 和 onItemLongClick 不起作用
【发布时间】:2013-11-06 06:14:42
【问题描述】:

我有一个安装了所有应用程序的列表视图。我需要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;
    }

我该如何解决?

【问题讨论】:

  • 你的错误是什么?在此处发布您的 logcat
  • 不是日志 cat 上的错误,而是语法:The method onItemLongClick(ListView, View, int, long) is undefined for the type ListActivity.. 我在我的 Main 类中实现了 OnItemLongClickListener,但如果我通过“MainActivity”,我会得到这个建议:The type MainActivity must implement the inherited abstract method AdapterView.OnItemLongClickListener.onItemLongClick(AdapterView&lt;?&gt;, View, int, long)跨度>
  • 你记得设置listView.setOnLongClickListener(this); ??

标签: java android android-listview uninstallation onlongclicklistener


【解决方案1】:

尝试实现这个

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

【讨论】:

  • 是的,他只是'覆盖了 setOnItemLongClickListener。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-12
  • 2018-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多