I'm building a list of contacts, where the user can select more than one one contact. Currently the android layouts only provide a multiple select with a single textview and a checkbox, what I want to do is have the name and number and a checkbox (two textviews and a checkbox). When I implement this with a custom layout, and when the user clicks on the list, the check boxes don't get ticked. I tried to bind the checkbox to the listview but it didnt work. Any help would be much appreciated.
Asked
Active
Viewed 5,377 times
2 Answers
0
private class EfficientAdapter extends BaseAdapter implements ListView.OnScrollListener
{
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return c.getCount();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.customcontactlist, null);
holder = new ViewHolder();
holder.txtName = (CheckedTextView) convertView.findViewById(R.id.TextView01);
holder.txtNumber = (TextView) convertView.findViewById(R.id.TextView00);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (hashtable.contains(position)) {
holder.txtName.setChecked(true);
}else{
holder.txtName.setChecked(false);
}
return convertView;
}
class ViewHolder {
CheckedTextView txtName;
TextView txtNumber;
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
}
and in Activity
l1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
EfficientAdapter.ViewHolder holder;
holder = (EfficientAdapter.ViewHolder) arg1.getTag();
if(holder.txtName.isChecked()){
holder.txtName.setChecked(false);
}else{
holder.txtName.setChecked(true);
}
}
});

Marek Sebera
- 39,650
- 37
- 158
- 244

Hades
- 3,916
- 3
- 34
- 74
0
I found a generic method to solve this kind of problem in a similar stackoverflow question ( Multiple choice list with custom view? ):
http://www.marvinlabs.com/2010/10/custom-listview-ability-check-items/