简体   繁体   中英

Update Custom ListView Row on selected ContextMenu Action

I have a custom ListView including a custom row_layout. After a longclick you get different options from the contextmenu. Depending on what option is chosen by the user I want to add an image/icon to the selected row in order to mark it.

I couldnt find an answer which covers exactly this use-case. I would appreciate a hint or some help or a tutorial for this case.

Thanks in advance.

Never mind I got to a solution by myself. I added a boolean variable to my ListItem - Class and the ImageView to the list_row_layout as well as a ImageView to my ViewHolder Class within the CustomAdapter class. So when a contextItem is clicked the boolean Attribute is set to true for the listItem which was clicked on.

Example Code:

@Override
public boolean onContextItemSelected(MenuItem item) {

        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

        int itemPosition = info.position;

        switch (item.getItemId()) {
            case R.id.contextItem1:

                listViewItems.get(itemPosition).setMarkerAttribute(true);
                listView.setAdapter(new MyCustomListAdapter(context, listViewItems));

                return true;
            case R.id.contextItem2:

//do sth
                return true;
            case R.id.contextItem3:
//do sth
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }

Hope this was useful to somebody, because a lot of examples only cover removing an item ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM