如何在ListActivity中使用onClick和onLongPress?
How to use onClick and onLongPress in ListActivity?
我想创建一个列表视图,我们可以从中使用 onclick 并在其中长按上下文菜单。代码是
public class MainActivity extends ListActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String quizlist[]={"Normal","MCQ 2 Options","MCQ 3 options","MCQ 4 Options"};
ArrayAdapter<String> ab=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,quizlist);
setListAdapter(ab);
}
}
提前致谢
您可以在 ListView
上注册一个 AdapterView.OnItemLongClickListener
。所以你需要做的就是找到列表视图:
在onCreate
尝试
((ListView) getView).setOnItemLongKlickListener(...)
或
((ListView) findViewById(<the id of your list view>).setOnItemLongKlickListener(...)
实施 OnItemLongClickListener
时,您必须重写 onItemLongClick
方法:
public abstract boolean onItemLongClick (AdapterView<?> parent, View view, int position, long id)
Added in API level 1 Callback method to be invoked when an item in
this view has been clicked and held. Implementers can call
getItemAtPosition(position) if they need to access the data associated
with the selected item.
Parameters
parent The AbsListView where the click happened
view The view within the AbsListView that was clicked
position The position of the view in the list
id The row id of the item that was clicked
Returns true if the callback consumed the long click, false otherwise
那么,parent
就是你的 ListView
。 view
是 *long clicked' 列表项的视图。 position
是列表中的位置,因此也是数组中的位置。对于 id
我不确定默认实现 returns 是常量还是 position
.
我想创建一个列表视图,我们可以从中使用 onclick 并在其中长按上下文菜单。代码是
public class MainActivity extends ListActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String quizlist[]={"Normal","MCQ 2 Options","MCQ 3 options","MCQ 4 Options"};
ArrayAdapter<String> ab=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,quizlist);
setListAdapter(ab);
}
}
提前致谢
您可以在 ListView
上注册一个 AdapterView.OnItemLongClickListener
。所以你需要做的就是找到列表视图:
在onCreate
尝试
((ListView) getView).setOnItemLongKlickListener(...)
或
((ListView) findViewById(<the id of your list view>).setOnItemLongKlickListener(...)
实施 OnItemLongClickListener
时,您必须重写 onItemLongClick
方法:
public abstract boolean onItemLongClick (AdapterView<?> parent, View view, int position, long id)
Added in API level 1 Callback method to be invoked when an item in this view has been clicked and held. Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
Parameters
parent The AbsListView where the click happened
view The view within the AbsListView that was clicked
position The position of the view in the list
id The row id of the item that was clicked
Returns true if the callback consumed the long click, false otherwise
那么,parent
就是你的 ListView
。 view
是 *long clicked' 列表项的视图。 position
是列表中的位置,因此也是数组中的位置。对于 id
我不确定默认实现 returns 是常量还是 position
.