我今天遇到的同样的问题。这就是我解决它的方法。我正在使用 RecyclerView 发布我的整个适配器类。
package com.aqua.machinetest;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Danish.sharma on 5/9/2017.
*/
public class GovernateListAdapter extends RecyclerView.Adapter<GovernateListAdapter.MyViewHolder> {
private List<Governate> governateList;
public static ArrayList<String> arrayListSelectedContacts = new ArrayList<>();
Context context;
public GovernateListAdapter(List<Governate> governateList, Context context) {
this.governateList = governateList;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.governate_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
Governate governate = governateList.get(position);
holder.title.setText(governate.getName());
holder.checkBox.setChecked(governateList.get(position).getCheckedBox());
holder.checkBox.setTag(governateList.get(position));
holder.checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Governate governate1 = (Governate) cb.getTag();
governate1.setCheckedBox(cb.isChecked());
governateList.get(position).setCheckedBox(cb.isChecked());
System.out.println("selected contacts list " + governate1.getId());
//Toast.makeText(v.getContext(), contact.getName() + " is " + cb.isChecked(), Toast.LENGTH_LONG).show();
if (cb.isChecked()) {
arrayListSelectedContacts.add(governateList.get(position).getId());
} else {
arrayListSelectedContacts.remove(governateList.get(position).getId());
}
System.out.println("selected contacts list 3 " + arrayListSelectedContacts);
Toast.makeText(context, arrayListSelectedContacts.size() + " Added", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return governateList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public CheckBox checkBox;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.textView_Title);
checkBox = (CheckBox) view.findViewById(R.id.checkBox);
}
}
}
这是我的 pojo 课:
package com.aqua.machinetest;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Danish.sharma on 5/9/2017.
*/
public class Governate {
String id;
String name;
Boolean checkedBox = false;
// List<Governate> governateList = new ArrayList<>();
public Governate() {
}
public Governate(String id, String name) {
this.id = id;
this.name = name;
// this.governateList = governateList;
}
protected Governate(Parcel in) {
id = in.readString();
name = in.readString();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getCheckedBox() {
return checkedBox;
}
public void setCheckedBox(Boolean checkedBox) {
this.checkedBox = checkedBox;
}
}
最后:这是我实现 onClickListener 的片段
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.linear_Next:
try {
if (GovernateListAdapter.arrayListSelectedContacts.size() == 0) {
Toast.makeText(getActivity(), "No data selected.", Toast.LENGTH_SHORT).show();
} else if (GovernateListAdapter.arrayListSelectedContacts.size() > 0) {
Toast.makeText(getActivity(), "Selected data=" + GovernateListAdapter.arrayListSelectedContacts.size(),
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}