I am developing Listview where Multiple selection is possible,now the problem is when i scroll down or up the selection of items get lost and selection made on any other item except the selected one.
Below is my Activity.
public class ContactPickerActivity extends Activity {
private ArrayList<Contact> arr = new ArrayList<Contact>();
private Context context;
private ListView list;
private ContactArrayAdapter adapter;
private String strName,strNumber;
private View view;
public static ArrayList<Boolean> arrBoolean = new ArrayList<Boolean>();
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.contact_picker);
ProgressDialog pd = new ProgressDialog(this);
list = (ListView)findViewById(R.id.contactList);
arr = new ArrayList<Contact>();
context = ContactPickerActivity.this;
arr = displayContacts();
Log.i("ContactPicker", "Completed Displaying Contact list ::: " + arr.size());
adapter = new ContactArrayAdapter(this,arr);
list.setAdapter(adapter);
// list.setAdapter(new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_multiple_choice, listContent));
list.setOnItemClickListener(ContactSelectedListener);
Log.i("Boolaean >>> ", arrBoolean.size() + "");
}
private OnItemClickListener ContactSelectedListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int arg2,long arg3) {
Log.i("ListViewTest","Item Click");
SparseBooleanArray checked = list.getCheckedItemPositions();
for (int i = 0; i < arr.size(); i++) {
Log.i("ListViewTest", arr.get(i)+ ": " + checked.get(i));
}
}
};
private ArrayList<Contact> displayContacts() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
Contact contact;
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
contact = new Contact();
String id = cur.getString(cur.getColumnIndex(People._ID));
String name = cur.getString(cur.getColumnIndex(People.DISPLAY_NAME));
contact.setName(name);
arr.add(contact);
}
}
return arr;
}
}
********* Adapter *****
public class ContactArrayAdapter extends BaseAdapter {
// private final List<Contact> list;
private Context context;
private LayoutInflater mInflater;
private List<Contact> list;
public ContactArrayAdapter(Context context,ArrayList<Contact> arrPublicData) {
this.mInflater = LayoutInflater.from(context);
this.list = arrPublicData;
}
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder viewHolder;
if (view == null) {
view = mInflater.inflate(R.layout.multiselect_row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.txtItem);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.chkItem);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Contact element = (Contact) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
ContactPickerActivity.arrBoolean.add(buttonView.isChecked());
Log.i("Boolaean 123 >>> ", buttonView.isChecked() + "");
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
viewHolder = (ViewHolder) view.getTag();
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
i m not able to get Items checked.... Any Help is appericiated...Thanx in Advance