我必须在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>
我无能为力。 如何选择全选,仅选择一个(我的意思是当用户选择一个,其他选择自动),选择全部但逐个。在此先感谢..
这是新代码
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
}
}
答案 0 :(得分:0)
为此,在 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());
}
});
它的工作正常。
答案 1 :(得分:0)
试试这个:
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;
}
}
你需要在你的活动/片段中找到一些东西,然后选择全部&#34;和&#34;更改选择模式&#34;。我有一个关于ListView的博客,http://programandroidlistview.blogspot.com/
希望这有帮助!