我目前有一项主要活动可以扩展ListActivity
我正在ListAdapter
使用数据库中的条目来夸大活动。我将膨胀的条目作为上下文菜单操作,但我希望能够在TextViews
单击选定的 ListView 时从其中一个内部获取值,这是我用OnListItemClick listener
.
问题是,当长按激活上下文菜单时,OnItemClickListener
不会注册,我无法ListView
从常规短按中获取值。的onListItemClick
具有View
单击时的可见性,但onContextItemSelected
没有,它仅具有 的可见性MenuItem
。
public class EntryActivity extends ListActivity
{
String currentItemName;
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
//The Value i need is this: currentItemName,
//and i need it to register when a list item is clicked
TextView curName =(TextView)v.findViewById(R.id.txtName) ;
currentItemName = curName.getText().toString();
}
//I need to use the String obtained from the click in the context menu
//to call a method, but a long click makes the onContextItemSelected
//be called, so onItemClickListener is never called, and i cant get the string
@Override
public boolean onContextItemSelected(MenuItem item )
{
switch(item.getItemId())
{
case ADD_ONE:
methodCalled(currentItemName);
break;
}
}
//I am inflating the list with a DataAdapter and a ListAdapter
private void refresh()
{
//create an array the size of the cursor(one item per row)
InventoryItem[] items = new InventoryItem[c.getCount()];
//create and set the DataAdaptor with the array of inventory items, to the
//inventoryList layout
da = new DataAdapter(this, R.layout.inventorylist, items);
setListAdapter(da);
}
}
有什么方法可以使我单击的视图对 contextMenu 侦听器可用?还是我以错误的方式解决这个问题?