来自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);
}
在这种情况下,ListView
的ID在XML中设置为android:id="@android:id/list"
。
然后在同一活动中:
@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;
}
但是当我长按列表中的某个项目时,唯一触发的是onListItemClick
。我从来没有通过长按获得消息。
答案 0 :(得分:1)
你不应该直接在你的onCreate()中实现像AdapterView.OnItemLongClickListener这样的接口。只需使用你的旧方法(你的类实现这个接口)并且每个方法覆盖,你必须这样写:< / p>
class User < ActiveRecord::Base
rolify
resourcify
end
---
the_boss = User.find(1)
bob = User.find_by(name: 'Bob')
# creating roles
the_boss.add_role(:manager) # add a global role
the_boss.add_role(:manager, bob) # add a role scoped to a user instance
# querying roles
bob.has_role?(:manager) # => false
the_boss.has_role?(:manager) # => true
the_boss.has_role?(:manager, bob) # => true
the_boss.has_role?(:manager, User.create) # => false
答案 1 :(得分:0)
我想我已经明白了,如果我遗失了某些东西,请有人纠正我。
我删除了“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;
}
});
虽然我不确定我是否应该在那里返回真假。
我还将长按功能改为
public boolean onListItemLongClick(View v, final int position, long id) {
Toast.makeText(this, "long clicked: " + mNames.get(position), Toast.LENGTH_LONG).show();
return true;
}
编辑:
更快的方法是将lv.setOnItemLongClickListener(this);
添加到OP中的onCreate方法。
答案 2 :(得分:0)
使用此。你的问题会解决。
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;
}
});