开发者

identifying id of listitem for contextmenu

开发者 https://www.devze.com 2022-12-29 14:26 出处:网络
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 edi

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

精彩评论

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