【发布时间】:2013-05-24 02:39:05
【问题描述】:
我目前有一个扩展 ListActivity 的主要活动
我正在使用带有数据库条目的ListAdapter 来夸大活动。
我将膨胀的条目作为上下文菜单运行,但我希望能够在单击选定的 ListView 时从其中一个 TextViews 中获取值,这是我通过 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 监听器可用?还是我以错误的方式解决这个问题?
【问题讨论】:
标签: android listadapter dataadapter android-contextmenu