使用文本菜单有列表视图中的android [英] Using contextmenu with listview in android
问题描述
我开发一个Android application.I将有一个ListView和我已经设置时才会出现一个列表视图项是长期pressed.How我会收到来自选定的ListView项的项目(比如文本上下文菜单从列表视图的TextView)从文本菜单进行操作后选择这样我就可以处理它? 下面是一些code:
I am developing an android application.I will have a listview and i have set a context menu to appear when a listview item is long-pressed.How do i get the item from the listview item selected(say text from a listview textview) after an action from the contextmenu is chosen so i can process it? Here is some code:
protected void onCreate(Bundle savedInstanceState) {
-------
lv1 = (ListView) findViewById(R.id.listings);
registerForContextMenu(lv1);
lv1.setOnItemClickListener(this);
}
而onCreateContextMenu:
And the onCreateContextMenu:
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case R.id.watch:
String name = "";
return true;
case R.id.buy:
return true;
default:
return super.onContextItemSelected(item);
}
}
我想从一个TextView文本列表item.How做我做到这一点?的
推荐答案
您应该注册 LISTVIEW
的上下文菜单。
you should register the LISTVIEW
for the context menu.
下面是源。
为的onCreate()
:
registerForContextMenu(lv);
和中长按进入选择的项目:
And to access the selected item during long click:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
if (v.getId() == R.id.lv) {
ListView lv = (ListView) v;
AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
YourObject obj = (YourObject) lv.getItemAtPosition(acmi.position);
menu.add("One");
menu.add("Two");
menu.add("Three");
menu.add(obj.name);
}
}
这篇关于使用文本菜单有列表视图中的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!