Uncheck CheckTextView from ListView so there are only two checked boxes at any time

时间:2016-04-15 14:53:46

标签: java android listview checkedtextview

I have a list view from which the user must choose two teams by checking the boxes and then validate by pressing the OK button. The problem is I want to make it so that there can only be two checked boxes at any time Example : if the user picks team 1 and 2 then the boxes for 1 and 2 should be checked but if the user then picks team 3, 1 should un-check automatically.

I've already managed to isolate and store the position of the box that needs to be unchecked but I don't know what to do with it.

Thanks!!

heres the listView

<Button
    android:id="@+id/btnConfirm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Ok"

    />

<ListView
    android:id="@+id/listEquipe"
    android:layout_below="@+id/btnConfirm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

and the following layout :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
    android:id="@+id/txtIdEquipe"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"/>
<CheckedTextView

    android:id="@+id/txtNomEquipe"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:padding="5dp"
    android:textSize="12pt"
    android:textColor="#000000"
    />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

这是实现它的一种方法(我在适配器中使用了愚蠢的数据):

public class MainActivity extends AppCompatActivity {

private List<Integer> selectedItems = new ArrayList<>();

private String[] teams = new String[]{ "Team A", "Team B", "Team C"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ListView lv = (ListView) findViewById(R.id.listEquipe);

    final BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return teams.length;
        }

        @Override
        public Object getItem(int position) {
            return teams[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = getLayoutInflater().inflate(R.layout.item, null);
            ((TextView) view.findViewById(R.id.txtIdEquipe)).setText((String) getItem(position));
            CheckedTextView ctv = (CheckedTextView) view.findViewById(R.id.txtNomEquipe);
            ctv.setChecked(selectedItems.contains(position));
            return view;
        }
    };

    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(selectedItems.contains(position)) {
                selectedItems.remove(position);
            }
            else {
                selectedItems.add(position);
                if(selectedItems.size() > 2) {
                    selectedItems.remove(0);
                }
            }

            adapter.notifyDataSetChanged();
        }
    });
}
}