简体   繁体   English

onItemLongClick是否未定义?

[英]onItemLongClick is undefined?

I'm trying to implement a long click in my listview item but it doesn't work and i get an error that says is undefined . 我试图在我的listview项目中实现长按,但是它不起作用,并且出现错误消息,即undefined Here's the code: 这是代码:

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

    }

Someone has an idea how solve the problem?Thanks 有人知道如何解决问题吗?

The reason for this is most likely that you don't implement the listener. 造成这种情况的原因很可能是您未implement侦听器。 Something like 就像是

public class ActivityName extends Activity implements OnItemLongClickListener{

Try changing 尝试改变

protected void setOnItemLongClickListener

to

protected boolean setOnItemLongClickListener{
      // your code
      return true;

You need to use the proper return type for the method which is boolean then return true so the listener knows it was a success. 您需要为该方法使用正确的返回类型,即boolean然后return true以便侦听器知道它是成功的。

Docs 文件

Please replace 请更换

public class MainActivity extends Activity  implements OnItemLongClickListener

and add unimplemented method 并添加未实现的方法

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

by default you can do this by right clicking on OnItemLongClickListener select Quick fix 默认情况下,您可以通过右键单击OnItemLongClickListener选择“快速修复”来执行此操作

try this listener for Listview : 试试这个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;
        }
    });

use this code 使用此代码

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

            //YOUR_CODE_HERE

            return false;
        }
    });

try to add this lines to your list adapter 尝试将这些行添加到列表适配器

        view.setOnLongClickListener(new View.OnLongClickListener() {

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

and the method is try to overwrite your method 方法是尝试覆盖您的方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM