简体   繁体   English

在选定的ContextMenu操作上更新自定义ListView行

[英]Update Custom ListView Row on selected ContextMenu Action

I have a custom ListView including a custom row_layout. 我有一个包括自定义row_layout的自定义ListView。 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. 我将一个布尔变量添加到我的ListItem-类中,并将ImageView添加到list_row_layout中,并且将ImageView添加到了CustomAdapter类中的ViewHolder类中。 So when a contextItem is clicked the boolean Attribute is set to true for the listItem which was clicked on. 因此,当单击contextItem时,对于单击的listItem,布尔属性设置为true。

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 ... 希望这对某人有用,因为许多示例仅涉及删除项目...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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