0

我正在尝试在我的列表视图项目中实现长按,但它不起作用,并且我收到一个错误,说 is 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();
            }

    }

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

4

5 回答 5

1

造成这种情况的原因很可能是您不是implement听众。就像是

public class ActivityName extends Activity implements OnItemLongClickListener{

尝试改变

protected void setOnItemLongClickListener

protected boolean setOnItemLongClickListener{
      // your code
      return true;

您需要为该方法使用正确的返回类型,boolean以便return true侦听器知道它是成功的。

文档

于 2013-10-23T12:43:08.977 回答
0

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;
}   
于 2013-10-23T13:07:48.173 回答
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 选择快速修复来执行此操作

于 2013-10-23T12:42:51.877 回答
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;
        }
    });
于 2013-10-23T12:44:19.150 回答
0

使用此代码

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

            //YOUR_CODE_HERE

            return false;
        }
    });
于 2013-10-23T12:46:29.160 回答