我使用的是ExpandableListView,同样的方式,他们在此示例代码做: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html如何确定谁触发了onContextItemSelected?
的ExpandableListView获取与类别和其子填充(一旦我点击一个类别)。例如: -Dairy(类别) -Milk(子类) -cheese(子类)
当我在牛奶或奶酪长单击,弹出一个菜单,使用该功能:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
ExpandableListContextMenuInfo info =(ExpandableListContextMenuInfo) menuInfo;
String selectedWord = ((TextView) info.targetView).getText().toString();
menu.setHeaderTitle(selectedWord.split(",")[1]); //set header
String itemId = selectedWord.split(",")[0];
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("action", "getProducts"));
nameValuePairs.add(new BasicNameValuePair("subcat_id", itemId));
String response = helper.makeHttpRequest(nameValuePairs);
String[] items = response.split(";");
for (int i=0; i<items.length; i++){
menu.add(0, 0, 0, items[i]);
}
}
然后,当我点击弹出菜单中的一个项目时,我想知道列表中的哪个项目被选中(如果我点击'牛奶'。例如,菜单中有“1%牛奶”, “2%牛奶”等._ 此功能被解雇:
@Override
public boolean onContextItemSelected(MenuItem item) {
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
String title = ((TextView) info.targetView).getText().toString();
String selected="";
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
Toast.makeText(this, title + " selected: " + selected+ " " + childPos + " clicked in group " + groupPos , Toast.LENGTH_SHORT).show();
return true;
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
Toast.makeText(this, title + " selected: " + selected + " " + childPos + " clicked in group " + groupPos , Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
但我没有找到一种方法来确定哪个项目完全在列表中被点击。
任何想法? 提前谢谢!如果我需要更清楚,请让我知道。
这正是我需要的,非常感谢你! – mila 2012-02-28 03:21:53