【问题标题】:Select Check Boxes in ListView[( Select multiple checkbox but one by one), Select All at once, Select only one at a time]选择 ListView 中的复选框[(一个一个地选择多个复选框),一次全选,一次只选择一个]
【发布时间】:2017-01-20 11:41:58
【问题描述】:

我必须在 ListView 中选择复选框。现在,当我选择复选框时,下面的一些复选框会自动选中。 这是我的代码:

StudentAdapter.java:

public class StudentAdapter extends BaseAdapter{
private Activity activity;
private List<StudentBean> studentBeanList;
private boolean checked = false;
CheckBox checkBox;
public StudentAdapter(Activity activity, List<StudentBean> studentBeanList){
    super();
    this.studentBeanList = studentBeanList;
    this.activity = activity;
}
@Override
public int getCount() {
    return studentBeanList.size();
}
@Override
public Object getItem(int position) {
    return studentBeanList.get(position);
}
@Override
public long getItemId(int position) {
    return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    StudentAdapter.ItemHolder itemHolder= new StudentAdapter.ItemHolder();
    if (convertView==null) {
        LayoutInflater li = (LayoutInflater)
                (activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE));

        convertView = li.inflate(
                R.layout.row_student, null);
       itemHolder.textViewStudent = (TextView) convertView
                .findViewById(R.id.tvstudentname);
        convertView.setTag(itemHolder);
        itemHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
        convertView.setTag(itemHolder);
    }
    else
    {
        itemHolder = (StudentAdapter.ItemHolder) convertView.getTag();

    }
    if (studentBeanList != null && !studentBeanList.isEmpty())
    {
        final StudentBean studentBean = studentBeanList.get(position);
        if (studentBean != null) {
            if
                    (itemHolder.textViewStudent != null && studentBean.getStudentname() != null)
            {
               itemHolder.textViewStudent.setText(studentBean.getStudentname());
            }
        }
    }
    return convertView;
}
private class ItemHolder {
    TextView textViewStudent;
    CheckBox checkBox;
}
@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
    // Your code to nofify
}
}

我在 XML 之下膨胀..

row_student.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:id="@+id/tvstudentname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:padding="10dp"
    android:layout_alignBaseline="@+id/checkBox1"
    android:layout_alignBottom="@+id/checkBox1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toLeftOf="@+id/checkBox1"
    android:layout_toStartOf="@+id/checkBox1" />
<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
 </RelativeLayout>

我无能为力。 我怎样才能得到全选,只选择一个(我的意思是当用户选择一个时,其他dis选择自动),一个接一个全选。提前谢谢..

这是新代码

 public class StudentAdapter extends BaseAdapter{
private Activity activity;
private StudentBean studentBean;
private List<StudentBean> studentBeanList;
public StudentAdapter(Activity activity, List<StudentBean> studentBeanList){
    super();
    this.studentBeanList = studentBeanList;
    this.activity = activity;

}

@Override
public int getCount() {
    return studentBeanList.size();
}

@Override
public Object getItem(int position) {
    return studentBeanList.get(position);
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    StudentAdapter.ItemHolder itemHolder= new StudentAdapter.ItemHolder();
    if (convertView==null) {
        LayoutInflater li = (LayoutInflater)
                (activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE));

        convertView = li.inflate(
                R.layout.row_student, null);

        itemHolder.textViewStudent = (TextView) convertView
                .findViewById(R.id.tvstudentname);
        convertView.setTag(itemHolder);
        itemHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
        convertView.setTag(itemHolder);
        itemHolder.checkBox.setChecked(studentBean.isChecked());
        itemHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                studentBean.setChecked(holder.checkBox.isChecked());
            }
        });
    }

    else

    {
        itemHolder = (StudentAdapter.ItemHolder) convertView.getTag();

    }

    if (studentBeanList != null && !studentBeanList.isEmpty())
    {
        final StudentBean studentBean = studentBeanList.get(position);
        if (studentBean != null) {
            if
                    (itemHolder.textViewStudent != null && studentBean.getStudentname() != null)
            {
                itemHolder.textViewStudent.setText(studentBean.getStudentname());
            }
        }
    }

    return convertView;
}

private class ItemHolder {
    TextView textViewStudent;
    CheckBox checkBox;
}

@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
    // Your code to nofify
}

}

【问题讨论】:

  • 你在列表视图中设置了 android:choiceMode="multipleChoice" 吗?
  • 我已经检查了这两个 ,android:choiceMode="multipleChoice" 和没有这个。在这两种情况下,listview 都会在一次单击中自动选择多个复选框。
  • 所有盒子还是其中一些?
  • 如果我选择了一个复选框,那么当我向下滚动时,它会自动选择其他框。 3 或 4 个盒子,因为我在 Listview 中有 60 个项目

标签: android listview checkbox custom-lists


【解决方案1】:

为此,您在 StudentBean pojo 类中添加一个布尔字段,并使默认布尔值的 getter setter 为 false。

private boolean checked = false;
   public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

现在在您的适配器中添加以下代码

 itemHolder.checkBox.setChecked(studentBean.isChecked());
 itemHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                studentBean.setChecked(holder.checkBox.isChecked());
            }
        });

它工作正常。

【讨论】:

  • java.lang.NullPointerException:尝试在 att.att.att 的空对象引用上调用虚拟方法“boolean att.att.att.com.attendance.bean.StudentBean.isChecked()” .com.attendance.adapter.StudentAdapter.getView(StudentAdapter.java:65) 显示这个错误
  • @ArjunSingh 请分享您添加该行的新代码
【解决方案2】:

试试这个:

public class StudentAdapter extends BaseAdapter{
private Activity activity;
private boolean multiSelection = true;
private int selectedPosition = -1;
private List<StudentBean> studentBeanList;
public StudentAdapter(Activity activity, List<StudentBean> studentBeanList){
    this.studentBeanList = studentBeanList;
    this.activity = activity;
}
@Override
public int getCount() {
    return studentBeanList.size();
}
@Override
public Object getItem(int position) {
    return studentBeanList.get(position);
}
@Override
public long getItemId(int position) {
    return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ItemHolder itemHolder;
    if (convertView == null) {
        LayoutInflater li = (LayoutInflater)
                (activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
        convertView = li.inflate(R.layout.row_student, null);
        itemHolder = new StudentAdapter.ItemHolder();
        itemHolder.textViewStudent = (TextView) convertView.findViewById(R.id.tvstudentname);
        itemHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
        convertView.setTag(itemHolder);
    } else {
        itemHolder = (StudentAdapter.ItemHolder) convertView.getTag();
    }
    itemHolder.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            CheckBox cb = (CheckBox) view;
            int pos = (Integer) cb.getTag();
            StudentBean studentBean = studentBeanList.get(pos);
            studentBean.setChecked(cb.isChecked());
            if(!multiSelection && selectedPosition != -1){
                StudentBean previousStudentBean = studentBeanList.get(selectedPosition);
                previousStudentBean.setChecked(false);
                notifyDataSetChanged();
                selectedPosition = pos;
            }
        }
    });
    if (studentBeanList != null && !studentBeanList.isEmpty()) {
        StudentBean studentBean = studentBeanList.get(position);
        if (studentBean != null) {
            if (itemHolder.textViewStudent != null && studentBean.getStudentname() != null) {
                itemHolder.textViewStudent.setText(studentBean.getStudentname());
                itemHolder.checkBox.setChecked(studentBean.isChecked());
            }
        }
    }
    itemHolder.checkBox.setTag(position);
    return convertView;
}
private class ItemHolder {
    TextView textViewStudent;
    CheckBox checkBox;
}
@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
    // Your code to notify
}
public void selectAll() {
    multiSelection = true;
    for(int i=0; i<studentBeanList.size(); i++){
        studentBeanList.get(i).setChecked(true);
    }
    notifyDataSetChanged();
}
public void changeSelectMode(boolean enableMultiSelection){
    multiSelection = enableMultiSelection;
}
}

您需要在“全选”和“更改选择模式”的活动/片段中包含一些内容。我有一个关于 ListView 的博客,http://programandroidlistview.blogspot.com/

希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 2013-10-21
    • 2021-01-12
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多