1

This problem is not new. However, any of the solution is not working for me.

I have got a ListView with several TextView which is populated from data fetched from SQLite. User can LongPress on one item to select multiple items of ListView and further choose to delete them from database.

What I want to do next is to highlight those items which are selected by user after the first LongPress (including the first one).
Here is the snippet of my ListView.

    <ListView
        ...
        android:drawSelectorOnTop="false"
        android:listSelector="@android:color/darker_gray"
        android:choiceMode="multipleChoice"/>

This is highlighting the item of ListView but not the first one on which the user will do the LongPress. And only highlight one of the item on ListView, whenever user chooses another item the current one is resetted and the background of the new item is changed, and this continues.

EDIT:

Here is my JAVA implementation of setMultiChoiceModeListener

list.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {

                final int checkedCount = list.getCheckedItemCount();
                mode.setTitle(checkedCount + " Selected");
                listViewAdapter.toggleSelection(position);

            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                mode.getMenuInflater().inflate(R.menu.hold_activity_actions, menu);
                return true;
            }

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

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                SparseBooleanArray selected = listViewAdapter.getSelectedItemIds();

                switch (item.getItemId()) {
                    case R.id.delete:
                        for (int i = (selected.size() - 1); i >= 0; i--) {
                            if (selected.valueAt(i)) {
                                bloodPressureRecords selectedItem = listViewAdapter.getItem(selected.keyAt(i));
                                // Remove selected items following the ids
                                listViewAdapter.remove(selectedItem);
                                db.deleteRecord(selectedItem);
                            }
                        }
                        mode.finish();
                        return true;
                    default:
                        return false;
                }

            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                listViewAdapter.removeSelection();
            }
        });
user3276435
  • 265
  • 1
  • 3
  • 13

2 Answers2

0

Try setting the background color of your textview on longClick. For Example:

textView.setBackgroundColor(Color.DKGRAY);
Priya B
  • 96
  • 10
0

Okay the problem is solved while doing some experiments with different ways suggested on SO.
Here is what I did to get what I wanted

Added this to my Relative Layout where I defined layout of ListView Item.

<RelativeLayout
        ...
        android:background="@drawable/rowbgselector">

Created a new resource file in drawable folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true"
        android:drawable="@android:color/darker_gray"/>
</selector>

That's all I did to get the required result.

user3276435
  • 265
  • 1
  • 3
  • 13