How to create a dynamic context menu in android?
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Selection Options");
menu.add(0, v.getId(), 0, "Remove");
}
I want my menu to say "Remove AAPL"
I would get the string AAPL from my array adapter, but I am not sure how I can access my array adapters index from thi开发者_开发知识库s method.
Example if you are using listviews with custom object:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
MyObject obj = (MyObject) myListView.getItemAtPosition(info.position);
menu.setHeaderTitle("Selection Options");
menu.add(0, v.getId(), 0, "Remove " + obj.name);
}
Cast menuInfo to an AdapterView.AdapterContextMenuInfo object. From there, you can get the position and id of the item in the ListView that was long-tapped.
加载中,请稍侯......
精彩评论