Clear SingleChoice ListView selection

后端 未结 5 1427
迷失自我
迷失自我 2020-12-29 10:13

Is there a way to clear the selected item in a ListView?

The ListView is defined like this:



        
相关标签:
5条回答
  • 2020-12-29 10:24

    Using listView.SetItemChecked(-1, true); works fine here.

    Here is my Activity I tested with:

    SetContentView(Resource.Layout.Main);
    var listView = FindViewById<ListView>(Resource.Id.listView);
    _listAdapter = new CustomListAdapter(this);
    listView.Adapter = _listAdapter;
    
    var button = FindViewById<Button>(Resource.Id.removeChoice);
    button.Click += (sender, args) => listView.SetItemChecked(-1, true);
    

    Main.axml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
        >
      <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice"
      />
      <Button
        android:id="@+id/removeChoice"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="remove choice"
        />
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-29 10:27

    Use clearChoices() to clear the checked state of all items in a ListView

    0 讨论(0)
  • 2020-12-29 10:38

    It's an old question, but just in case someone else needs it in the future, based on @Cheesebaron answer, here's what I did:

    On each ListViews' OnItemClickListener set the other's list checked item to false:

    list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                list2.setItemChecked(list2.getSelectedItemPosition(), false);
            }
    });
    

    This implementation is in Java, but if you get the logic here you can implement it in C# as well. Hope this helps.

    0 讨论(0)
  • 2020-12-29 10:41

    Its works simple for me:

    ListView listView = (ListView) findViewById(R.id.idMyListView);
             listView.clearFocus();
    
    0 讨论(0)
  • 2020-12-29 10:46

    For CHOICE_MODE_SINGLE / singleChoice

    int v = listView.getCheckedItemPosition();
    if (v >= 0)
        listView.setItemChecked(v, false);
    
    0 讨论(0)
提交回复
热议问题