更改所选ListView项目的图像

时间:2022-11-21 23:02:04

I'm using the method below to select multiple items in the list and get delete them. I would like to change the icons image of the item I selected, however I did not find at what time I can do this, can anyone help me?

我正在使用下面的方法选择列表中的多个项目并删除它们。我想更改我选择的项目的图标图像,但是我没有发现我什么时候可以做到这一点,有人可以帮助我吗?

listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listview.setMultiChoiceModeListener(new MultiChoiceModeListener() {

        @Override
        public void onItemCheckedStateChanged(ActionMode actionMode, int position, long id, boolean checked) {
            final int checkedCount = listview.getCheckedItemCount();
            actionMode.setTitle(checkedCount + getString(R.string.selected_item_list));
            adapter.toggleSelection(position);
        }

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.delete:
                    SparseBooleanArray selected = adapter.getSelectedIds();
                    for (int i = (selected.size() - 1); i < 0; i--) {
                        if (selected.valueAt(i)) {
                            Object object = adapter
                                    .getItem(selected.keyAt(i));
                            adapter.remove(delivery);
                        }
                    }
                    actionMode.finish();
                    return true;
                default:
                    return false;
            }
        }

        @Override
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            MenuInflater inflater = actionMode.getMenuInflater();
            inflater.inflate(R.menu.menu_home_delete, menu);
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode actionMode) {
            adapter.removeSelection();
        }

        @Override
        public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
            return false;
        }
    });

1 个解决方案

#1


0  

Add a boolean to the data object in your adapter. Suppose you've got ArrayAdapter. Add some kind of "selected" field in the MyDataObject, and toggle it when you "select" the row.

将一个布尔值添加到适配器中的数据对象。假设你有ArrayAdapter。在MyDataObject中添加某种“选定”字段,并在“选择”该行时切换它。

Override getView in the adapter (you'll need a custom Adapter, btw. I'd just extend ArrayAdapter). When you render the row, if the "selected" field is true, show the "other" image.

覆盖适配器中的getView(你需要一个自定义适配器,顺便说一下。我只是扩展了ArrayAdapter)。渲染行时,如果“selected”字段为true,则显示“other”图像。

When you click the button, call 'notifyDataSetChanged' on the adapter. The will cause the visible rows to refresh themselves (and call getView for each).

单击该按钮时,请在适配器上调用“notifyDataSetChanged”。这将导致可见行自行刷新(并为每个行调用getView)。

Since you only want images to change when you click the button, you'll need to have some kind of global boolean, so the getView won't show the image until the button has been clicked.

由于您只需要在单击按钮时更改图像,因此您需要具有某种全局布尔值,因此在单击按钮之前,getView将不会显示图像。

#1


0  

Add a boolean to the data object in your adapter. Suppose you've got ArrayAdapter. Add some kind of "selected" field in the MyDataObject, and toggle it when you "select" the row.

将一个布尔值添加到适配器中的数据对象。假设你有ArrayAdapter。在MyDataObject中添加某种“选定”字段,并在“选择”该行时切换它。

Override getView in the adapter (you'll need a custom Adapter, btw. I'd just extend ArrayAdapter). When you render the row, if the "selected" field is true, show the "other" image.

覆盖适配器中的getView(你需要一个自定义适配器,顺便说一下。我只是扩展了ArrayAdapter)。渲染行时,如果“selected”字段为true,则显示“other”图像。

When you click the button, call 'notifyDataSetChanged' on the adapter. The will cause the visible rows to refresh themselves (and call getView for each).

单击该按钮时,请在适配器上调用“notifyDataSetChanged”。这将导致可见行自行刷新(并为每个行调用getView)。

Since you only want images to change when you click the button, you'll need to have some kind of global boolean, so the getView won't show the image until the button has been clicked.

由于您只需要在单击按钮时更改图像,因此您需要具有某种全局布尔值,因此在单击按钮之前,getView将不会显示图像。