onItemLongClick未定义?

时间:2013-10-23 12:40:58

标签: java android android-listview onlongclicklistener long-click

我正在尝试在listview项目中实现长按,但它不起作用,我收到的错误是undefined。这是代码:

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

        ApplicationInfo app = applist.get(position);
        try {
            Intent intent = packageManager
                    .getLaunchIntentForPackage(app.packageName);

            if (null != intent) {
                startActivity(intent);
            }
            } catch (ActivityNotFoundException e) {
                Toast.makeText(MainActivity.this, e.getMessage(),
                        Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(MainActivity.this, e.getMessage(),
                        Toast.LENGTH_LONG).show();
            }

    }

有人知道如何解决问题?谢谢

5 个答案:

答案 0 :(得分:1)

原因很可能是你没有implement听众。像

这样的东西
public class ActivityName extends Activity implements OnItemLongClickListener{

尝试更改

protected void setOnItemLongClickListener

protected boolean setOnItemLongClickListener{
      // your code
      return true;

您需要为boolean然后return true使用正确的返回类型,以便听众知道它是成功的。

Docs

答案 1 :(得分:0)

请替换

public class MainActivity extends Activity  implements OnItemLongClickListener

并添加未实现的方法

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

默认情况下,您可以通过右键单击OnItemLongClickListener选择快速修复

来执行此操作

答案 2 :(得分:0)

尝试Listview的监听器:

istView.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            Toast.makeText(arg0.getContext(), ((TextView)arg1).getText(), Toast.LENGTH_SHORT).show();
            return false;
        }
    });

答案 3 :(得分:0)

使用此代码

yourListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            //YOUR_CODE_HERE

            return false;
        }
    });

答案 4 :(得分:0)

尝试将此行添加到列表适配器

        view.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                return false;
            }
        });

,方法是尝试覆盖您的方法

@Override
public boolean onItemLongClick(
        AdapterView<?> parent, View view,
        int position, long id) {
    // TODO Auto-generated method stub
    return false;
}