简体   繁体   English

为什么我的onitemlongclick监听器不起作用?

[英]Why isn't my onitemlongclick listener working?

From MainActivity: 从MainActivity:

public class MainActivity extends ListActivity implements AdapterView.OnItemLongClickListener {
    private DataSourceSql mDataSourceSql;
    protected ArrayList<String> mProfileNames;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDataSourceSql = new DataSourceSql(MainActivity.this);
        mNames = new ArrayList<String>();

        //this part here
        ListView lv = (ListView) findViewById(android.R.id.list);
        lv.setLongClickable(true);
    }

The id of the ListView in this case is set in the XML as android:id="@android:id/list" . 在这种情况下, ListView的ID在XML中设置为android:id="@android:id/list"

And then later on in the same activity: 然后在同一活动中:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Toast.makeText(this, mNames.get(position), Toast.LENGTH_SHORT).show();
}

@Override
public boolean onItemLongClick(AdapterView<?> l, View v, final int position, long id) {
    Toast.makeText(this, "long clicked: " + mNames.get(position), Toast.LENGTH_LONG).show();
    return true;
}

But when I long-press an item in the list, the only thing that triggers is onListItemClick . 但是,当我长按列表中的一个项目时,唯一触发的事件是onListItemClick I never get the message with the long click. 长按我永远不会收到消息。

You shouldn't implement an interface like AdapterView.OnItemLongClickListener directly in your onCreate().. Just use your old way (Your class implement this interface) and with each method override, you have to write like this: 您不应该直接在onCreate()中实现类似AdapterView.OnItemLongClickListener的接口。仅使用旧方法(您的类实现了此接口),并且必须重写每个方法,如下所示:

lv.setOnItemLongClickListener(this);
lv.setonListItemLongClickListener(this);

I think I figured it out, but someone please correct me if I'm missing something. 我想我已经解决了,但是如果我缺少某些东西,请有人纠正我。

I removed the "implements..." thing and then added the following to the onCreate method: 我删除了“ implements ...”,然后将以下内容添加到onCreate方法中:

lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener
                () {
            @Override
            public boolean onItemLongClick(AdapterView<?> av, View v, int
                    pos, long id) {
                onListItemLongClick(v, pos, id);
                return false;
            }
        });

Although I am not sure if I should be returning true or false there. 尽管我不确定是否应该在此处返回true或false。

I also changed the long click function down below to 我还将下面的长按功能更改为

public boolean onListItemLongClick(View v, final int position, long id) {
    Toast.makeText(this, "long clicked: " + mNames.get(position), Toast.LENGTH_LONG).show();
    return true;
}

Edit: 编辑:

Quicker approach is to just add lv.setOnItemLongClickListener(this); 更快的方法是只添加lv.setOnItemLongClickListener(this); to my onCreate method in the OP. 到我在OP中的onCreate方法。

Use This. 用这个。 Your Problem will Solve. 您的问题将解决。

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "Click", Toast.LENGTH_SHORT).show();
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(MainActivity.this, "Long Click", Toast.LENGTH_SHORT).show();
                return true;
            }
        });

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

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