I have Custom List to display image with it’s name like below…
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private String[] data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, String[] d) {
activity = a;
data=d;
inflater =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView text;
public ImageView image,yesimage;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.item, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.text);;
holder.image=(ImageView)vi.findViewById(R.id.image);
holder.yesimage=(ImageView)vi.findViewById(R.id.selectedyes);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.text.setText("item "+position);
holder.image.setTag(data[position]);
holder.yesimage.setTag(data[position]);
imageLoader.DisplayImage(data[position], activity, holder.image,holder.yesimage);
return vi;
}
}
for this list I’m trying to set,
list=(ListView)findViewById(R.id.list);
adapter=new LazyAdapter(this, mStrings);//mstrings is array
list.setAdapter(adapter);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
where as I’m able to get all checked list items using,
list.setOnItemClickListener(new OnItemClickListener(){
TextView selected=(TextView)findViewById(R.id.selected);
final ArrayList<String> years = new ArrayList<String>();
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
SparseBooleanArray checked = list.getCheckedItemPositions();
if(checked.get(arg2))
{
years.add(""+mStrings[arg2]+"\n");
}
else
{
years.remove(" "+mStrings[arg2]+"\n");
}
if(years.size()!=0)
selected.setText(years.toString());
else
selected.setText("You Have Nothing in Cart");
}});
but nothing indicates in list that the selected item is checked, it just considers clicked item as selected,Here I cannot use,
list.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, mStrings));
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Because my list Extends BaseAdapter,
I want to put the checkbox for every row in the list, to ensure that the item is checked once I check that list item, how can I do that…?
Take CheckBox inside the ViewHolder and implement setOnCheckedChangeListener for each check box.
for example: