【发布时间】: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