package com.app.FragmentAdapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import com.addresslist.R;
import com.app.bean.ContactsBean;
;import java.util.ArrayList;
import java.util.List;
/**
- Created by dell-1 on 2018/1/9. */
public class AddContactFromAddressAdapter extends BaseAdapter {
private List contacts;
private ArrayList contactsList=new ArrayList<>();
private ArrayList selectedList;
;
private LayoutInflater inflater;
public AddContactFromAddressAdapter(Context context, List<ContactsBean> contacts) {
this.contacts = contacts;
inflater = LayoutInflater.from(context);
selectedList=new ArrayList();
}
@Override
public int getCount() {
return contacts.size();
}
@Override
public Object getItem(int position) {
return contacts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.contacts_cell, null);
holder = new ViewHolder();
holder.sort_key = convertView.findViewById(R.id.tv_sort_key);
holder.mPhones = convertView.findViewById(R.id.tv_phones);
holder.checkBoxSelected = convertView.findViewById(R.id.cb_selected);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.sort_key.setText(contacts.get(position).getFirstLetterOfName());
holder.mPhones.setText(contacts.get(position).getContactsName() + ":"
+ contacts.get(position).getPhoneNumber());
if (contacts.get(position).getSelectedContact()) {
holder.checkBoxSelected.setChecked(true);
} else {
holder.checkBoxSelected.setChecked(false);
}
holder.checkBoxSelected.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (contacts.get(position).getSelectedContact()) {
contacts.get(position).setSelectedContact(false);
selectedList.remove(contacts.get(position));
} else {
contacts.get(position).setSelectedContact(true);
selectedList.add(contacts.get(position));
}
}
});
return convertView;
}
private final class ViewHolder {
public TextView sort_key;
public TextView mPhones;
public CheckBox checkBoxSelected;
}
}