我有以下代码:
public class MainActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
}}
我需要将其更改为 onListItemLongClick(),但如何?有可能吗?
答案 0 :(得分:6)
您的问题与this one非常相似,但看起来并不完全相同。
你注意到的是ListActivity类没有专门针对这种情况的方法覆盖。
为了将此功能添加为方法覆盖,您的类应实现AdapterView.OnItemLongClickListener
界面,然后您可以添加onItemLongClick()
方法覆盖,其行为与onListItemClick()
一样方法覆盖你已经拥有,但响应长按。
请确保您遵循this answer中的说明,必须在布局xml中使用android:longClickable="true"
,或致电listview.setLongClickable(true);
示例:
public class MainActivity extends ListActivity implements AdapterView.OnItemLongClickListener {
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview = (ListView) findViewById(R.id.list);
listview.setLongClickable(true);
}
@Override
public boolean onItemLongClick(AdapterView<?> l, View v,
final int position, long id) {
Toast.makeText(this, "long clicked pos: " + position, Toast.LENGTH_LONG).show();
return true;
}
protected void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(this, "short clicked pos: " + position, Toast.LENGTH_LONG).show();
}
//....................
答案 1 :(得分:3)
您只需使用[{"ProjectName": "AAA",
"Emp": [
{
"EmpID": {
"$oid": "123"
},
"Rating": 4
}
]},
{
"ProjectName": "BBB","Emp": []
}]
Pro.aggregate([
{ $unwind: "$Emp" },
{ $group: {
"_id": '$_id',
"ProjectName": { "$first": "$ProjectName" },
}},
], function (err, result) {
});
答案 2 :(得分:2)
试试这个
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View v,
int index, long arg3) {
// write your code
return false;
}});
答案 3 :(得分:1)
您可以实现侦听器,然后回调将在您的类中显示为函数(方法):
public class MainActivity extends ListActivity implements View.OnLongClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View listView = findViewById(R.id.list_view);
listView.setOnLongClickListener(this);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
@Override
public boolean onLongClick(View v) {
// Do your work here
return false;
}
}
答案 4 :(得分:-1)
是的,有可能并且很容易实现, 只需要将列表视图的实例设为
ListView lv = (ListView)findViewById(R.id.listview);
lv.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View
// your code here
return true;
}
});)
在您的情况下,您可以在xml中创建列表视图而不是扩展ListActivity,并在MainActivity中查找视图,并按照上述步骤操作。