-1

I've been trying to unselect my listView selection, and have tried all the methods listed online, but it simply won't unselect:

<ListView
    android:layout_marginTop="15dp"
    android:layout_width="wrap_content"
    android:layout_height="169dp"
    android:id="@+id/listview"
    android:choiceMode="singleChoice"
    android:listSelector="#666666">

Now when I want to unselect it, I have the following code:

listView.setItemChecked(position, false);
listView.clearChoices();
listView.requestLayout();
listView.setSelected(false);


adapter.notifyDataSetChanged();

However, the selected item in the listView stays grayed out in color (#666666) and doesn't unselect.

How can I unselect a selected item from my listView?

Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • Possible duplicate of [Clear SingleChoice ListView selection](http://stackoverflow.com/questions/15081274/clear-singlechoice-listview-selection) – Mike M. Oct 15 '16 at 21:26
  • @MikeM. Not a duplicate at all, as my question mentions using all of those solutions, but it is still not working for me... – Ruchir Baronia Oct 15 '16 at 22:06
  • Please indicate exactly where in your question you mention trying `listView.setItemChecked(-1, true);`. Anyway, I tested both that solution, and `clearChoices()` followed by `notifyDataSetChanged()` on the `Adapter`, and they both worked for me. – Mike M. Oct 15 '16 at 22:24
  • @MikeM. In the second block of code, but I use "position", not -1. – Ruchir Baronia Oct 15 '16 at 22:26
  • I don't think you're following me, here. Notice that your call is trying to un-select an existing position. `listView.setItemChecked(-1, true)` is selecting the `-1` position. Please look closely. – Mike M. Oct 15 '16 at 22:28
  • @MikeM. This seems to work on 4.4 but not on 6.0. Do you know why? – Ruchir Baronia Oct 24 '16 at 19:03
  • No, I don't. I was on 4.4 when I tested this, so it may very well be the case that they've changed that behavior. – Mike M. Oct 25 '16 at 00:20

1 Answers1

0

Here is the solution

ListView listPerkings = (ListView) findViewById(R.id.perkiings);
    listPerkings .setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    SparseBooleanArray checked = new SparseBooleanArray();
    listPerkings .setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub

            TextView text1 = (TextView) view.findViewById(R.id.perkLabel);
            int len = listPerkings .getCount();
            checked = listPerkings .getCheckedItemPositions();
            for (int i = 0; i < len; i++) {
                if (i == position) {
                    if (checked.get(i)) {
                        text1.setTextColor(getResources().getColor(
                                R.color.perks_selected_item_text_color));
                    } else {
                        text1.setTextColor(getResources().getColor(
                                R.color.perks_default_item_text_color));
                    }
                }
            }
        }
    });
Rajakumar
  • 907
  • 1
  • 7
  • 17