Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将复选框(一次只能选中一个)添加到自定义列表视图_Java_Android_Checkbox_Android Listview_Listadapter - Fatal编程技术网

Java 将复选框(一次只能选中一个)添加到自定义列表视图

Java 将复选框(一次只能选中一个)添加到自定义列表视图,java,android,checkbox,android-listview,listadapter,Java,Android,Checkbox,Android Listview,Listadapter,我已经制作了我的定制适配器,正在改编一系列“人物”。在我的列表项xml中,我放了一个复选框,但这就是我感到困惑的地方。我希望我的列表一次只能选中一个复选框。我还想实现某种类型的OnCheckBox click侦听器,以便在每次选中不同的复选框时执行一个操作 这是我的自定义适配器 public class PersonAdapter extends BaseAdapter { Context mContext; Person[] mPersonArray; public PersonAdap

我已经制作了我的定制适配器,正在改编一系列“人物”。在我的列表项xml中,我放了一个复选框,但这就是我感到困惑的地方。我希望我的列表一次只能选中一个复选框。我还想实现某种类型的OnCheckBox click侦听器,以便在每次选中不同的复选框时执行一个操作

这是我的自定义适配器

 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"/>

听起来你要找的是单选按钮而不是复选框


复选框的设置使您能够选择一个类别中的多个项目,单选按钮的设置使您只能单击该类别中的一个。

我在我的项目中使用了该选项,您也可以尝试,但是如果您必须一次选中一个复选框,那么为什么要使用复选框,您必须使用radiobutton

 // 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;
        }

使用成员变量存储当前选定的索引,为每个更新该索引的复选框注册一个侦听器,并在
getView
中使用它设置当前复选框状态。我不同意Visionwriter和cchapman的观点,在下面发布了答案。ListView上的行项目有其自己的视图。它们不能像单选按钮或一组单选按钮那样分组。但是我认为您应该更改使用复选框的UI设计。通常用户可以选择多个复选框,多个复选框。这是趋势,不应偏离。作为建议,当用户选择某一行项目时,如何突出显示该行项目,并且只能突出显示一个项目?我这样做了,效果非常好,谢谢!!那么现在你能帮我下一步会发生什么吗。在扩展ListAdapter的我的类中,如果我想记录已选择的项目,我在这里做什么?我试图实现oncheckchangedlistener。。。但是我很困惑。这条路对吗?感谢@Surender KumarSorry为latee所做的一切,但实际上我对此一无所知,因为我从未在listadapter上工作过。
 // 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;
        }