public class SmsDataAdapter extends ArrayAdapter {
private Context context;
private ArrayList lists;
CheckBox cb;
public SmsDataAdapter(Context context, ArrayList lists) {
super(context,android.R.layout.simple_list_item_1,lists);
this.context = context;
this.lists = lists;
}
public ArrayList getCheckedItems(){
ArrayList checkedList = new ArrayList<>();
for (SmsData smsData : lists){
if (smsData.isChecked()) {
checkedList.add(smsData);
}
}
return checkedList;
}
@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v==null){
v = LayoutInflater.from(context).inflate(R.layout.list_item_view,null,false);
}
TextView tv_name = (TextView) v.findViewById(R.id.tv_name);
TextView tv_phone = (TextView) v.findViewById(R.id.tv_phone);
TextView tv_content = (TextView) v.findViewById(R.id.tv_content);
cb= (CheckBox) v.findViewById(R.id.checkBox);
SmsData smsData = lists.get(position);
cb.setChecked(smsData.isChecked());
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (cb.isChecked()){
lists.get(position).setChecked(true);
}
}
});
tv_name.setText(smsData.getName());
tv_phone.setText(smsData.getPhone());
tv_content.setText(smsData.getSms());
if (smsData.isSent()){
tv_content.setTextColor(Color.BLUE);
}else {
tv_content.setTextColor(Color.BLACK);
}
return v;
}
public void setAllChecked(){
for (SmsData smsData:lists){
smsData.setChecked(true);
}
notifyDataSetChanged();
}
public void clearAllChecked(){
for (SmsData smsData:lists){
smsData.setChecked(false);
}
notifyDataSetChanged();
}
public int getCheckedCount(){
int count = 0;
for (int i = 0;i<lists.size();i++){
if (lists.get(i).isChecked()) {
count++;
}
}
return count;
}
}