代码之家  ›  专栏  ›  技术社区  ›  Wildblue09

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

  •  1
  • Wildblue09  · 技术社区  · 11 年前

    我已经制作了我的自定义适配器,正在调整一系列“人”。在我的列表项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"/>
    

    在我的班级里,我正在填充列表视图。。。。它扩展了ListAdapter

     Person[] personArray = new Person[mPersonArrayList.size()];
        personArray = mPersonArrayList.toArray(personArray);
    
        PersonAdapter adapter = new PersonAdapter(this , personArray);
        setListAdapter(adapter);
    
    2 回复  |  直到 11 年前
        1
  •  1
  •   Surender Kumar    11 年前

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

     // 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;
            }
    
        2
  •  0
  •   cchapman Tim Wan    11 年前

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

    复选框用于选择一个类别的多个项目,单选按钮用于仅单击其中一个类别。