在我的自定义列表视图中添加一个复选框(一次只能选中一个)

Adding a check box (only one can be checked at a time) to my custom list view

我已经制作了我的自定义适配器并正在调整 "persons" 的数组。在我的列表项 xml 中,我放了一个复选框,但这是我感到困惑的地方。我希望我的列表一次只能选中一个复选框。而且我还想实现某种 OnCheckBox 单击侦听器,以便在每次选择不同的复选框时执行操作。

这是我的自定义适配器

 public class PersonAdapter extends BaseAdapter {

Context mContext;
Person[] mPersonArray;

public PersonAdapter(Context context , Person[] persons) {
    mContext = context;
    mPersonArray = persons;
}

@Override
public int getCount() {
    return mPersonArray.length;
}

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

@Override
public long getItemId(int position) {
    return 0;        // we wont use
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        // brand new
        convertView = LayoutInflater.from(mContext).inflate(R.layout.person_list_item , null);
        holder = new ViewHolder();
        holder.nameText = (TextView) convertView.findViewById(R.id.nameTextView);
        holder.birthDateText = (TextView) convertView.findViewById(R.id.birthDateTextView);
        holder.checkBoxList = (CheckBox) convertView.findViewById(R.id.checkBox);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    Person personForList = mPersonArray[position];

    holder.nameText.setText(personForList.getName());
    holder.birthDateText.setText(personForList.getBirthDate());





    return convertView;
}

public static class ViewHolder {
    TextView nameText;
    TextView birthDateText;
    CheckBox checkBoxList;
}

这是我的列表项xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:background="#ff53">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Evan Dix"
    android:id="@+id/nameTextView"
    android:textColor="#ff323232"
    android:textSize="30sp"
    android:layout_marginLeft="50dp"
    android:layout_marginStart="86dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="11-12-12"
    android:id="@+id/birthDateTextView"
    android:textColor="#ff323232"

    android:layout_marginStart="23dp"
    android:layout_centerVertical="true"
    android:layout_toEndOf="@+id/imageView"/>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:src="@android:drawable/presence_invisible"
    android:layout_centerVertical="true"
    android:layout_toEndOf="@+id/nameTextView"
    android:layout_marginStart="12dp"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/checkBox"
    android:layout_centerVertical="true"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="29dp"
    android:checked="false"
    android:clickable="true"
    android:choiceMode = "singleChoice"/>

在我填充列表视图的 class 中....它扩展了 ListAdapter

 Person[] personArray = new Person[mPersonArrayList.size()];
    personArray = mPersonArrayList.toArray(personArray);

    PersonAdapter adapter = new PersonAdapter(this , personArray);
    setListAdapter(adapter);

听起来您要查找的是单选按钮而不是复选框。

制作复选框以便您能够 select 一个类别的多个项目,制作单选按钮以便您只能单击其中一个类别。

我在我的项目中使用过这个,你也可以试试,但是如果你必须一次选中一个复选框那么你为什么要使用复选框你必须使用单选按钮。

 // initialize selectPosition in your base adapter
    int selectedPosition =   -1; 

    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if (convertView == null) {
                // brand new
                convertView = LayoutInflater.from(mContext).inflate(R.layout.person_list_item , null);
                holder = new ViewHolder();
                holder.nameText = (TextView) convertView.findViewById(R.id.nameTextView);
                holder.birthDateText = (TextView) convertView.findViewById(R.id.birthDateTextView);
                holder.checkBoxList = (CheckBox) convertView.findViewById(R.id.checkBox);

                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            Person personForList = mPersonArray[position];

            holder.nameText.setText(personForList.getName());
            holder.birthDateText.setText(personForList.getBirthDate());
          holder.checkBoxList.setChecked(position == selectedPosition);
                 holder.checkBoxList.setTag(position);
        holder.checkBoxList.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        selectedPosition = (Integer) view.getTag();

                        notifyDataSetInvalidated();
                        notifyDataSetChanged();
                    }
                });




            return convertView;
        }