开发者

identifying id of listitem for contextmenu

I have a View that extends Activity. A ListView will display a number of listi开发者_开发技巧tems. When the user long clicks I would like to present them with a contextmenu allowing them to select edit, delete etc... and then identify the listitem that was selected as the item to perform the action on.

In onCreate I have:

listView.setAdapter(adapter);
listView.setOnItemClickListener(onListClick);
listView.setOnItemLongClickListener(onListLongClick);
registerForContextMenu(listView);

I have a method onCreateContextMenu

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Context Menu");
    menu.add(0, v.getId(), 0, "Edit");
    menu.add(0, v.getId(), 0, "Delete");

}

and also onContextItemSelected

@Override
public boolean onContextItemSelected(MenuItem item) {

    if (item.getTitle() == "Edit") {
        // edit action
    } else if (item.getTitle() == "Delete") {
        // delete action
    } else {
        return false;
    }
    return true;
}

I am not sure where to go from here to get the correct row/listitem.


You can use AdapterContextMenuInfo please refer Link-1 from the code you provided,

public boolean onContextItemSelected(MenuItem item) {

    if (item.getTitle() == "Edit") {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        editInfo(info.position);
    } else if (item.getTitle() == "Delete") {
        // TODO Delete action
    } else {
        return false;
    }

    return true;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜