I'm trying to set up a listview with four different layouts for each type of row (4 types of row), i'm using ViewHolder.
But i'm getting a weird problem which i'm not able to fix up.
My views (all 4), have a CheckBox, some of the CheckBox have to be marked and disabled, when I have this listview with many rows and i scroll througth it, my CheckBox are getting checked and unchecked, enabled and disabled...
I belive my problem has something to do with recycling views, but i'm not getting what can it be.
Here i post some of my code:
class MyAdapter extends BaseAdapter {
private FragmentActivity activity;
private List<Extra> data;
private static LayoutInflater inflater=null;
private ManifestData manifest;
private Context context;
final List<ExtraRow> rows;
MyAdapter(List<Extra> extras, FragmentActivity activity, Context context) {
rows = new ArrayList<ExtraRow>();
this.activity = activity;
this.context = context;
for (Extra e : extras) {
if (e.getPriceType()==1) {
rows.add(new ExtraRowType1(LayoutInflater.from(context), e, activity));
} else if (e.getPriceType()==2){
rows.add(new ExtraRowType2(LayoutInflater.from(context), e, activity));
} else if (e.getPriceType()==3){
rows.add(new ExtraRowType3(LayoutInflater.from(context), e, activity));
} else if (e.getPriceType()==4){
rows.add(new ExtraRowType4(LayoutInflater.from(context), e, activity));
}
}
}
@Override
public int getViewTypeCount() {
return 4;
}
@Override
public int getItemViewType(int position) {
return rows.get(position).getViewType();
}
public int getCount() {
return rows.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
return rows.get(position).getView(convertView);
}
}
One of my rows (the others are very similar but with some UI changes):
public class ExtraRowType3 implements ExtraRow{
private final Extra extra;
private final LayoutInflater inflater;
final FragmentActivity activity;
private ViewHolder holder;
public ExtraRowType3(LayoutInflater inflater, Extra extra, FragmentActivity activity) {
this.extra = extra;
this.inflater = inflater;
this.activity = activity;
}
@Override
public View getView(View convertView) {
View view;
if (convertView == null) {
ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.reservas_extras_3, null);
holder = new ViewHolder((TextView)viewGroup.findViewById(R.id.reservas_extras_titulo),
(CheckBox)viewGroup.findViewById(R.id.reservas_extras_3_checkbox),
(TextView)viewGroup.findViewById(R.id.reservas_extras_total));
viewGroup.setTag(holder);
view = viewGroup;
} else {
holder = (ViewHolder)convertView.getTag();
view = convertView;
}
holder.title.setText(extra.getNombre());
if(extra.isObligatorio()){
holder.checkBox.setChecked(true);
holder.checkBox.setClickable(false);
}else{
Disponibilidad d = Disponibilidad.getInstance();
if(d.getExtraReserva(extra.getId())!=null){
holder.checkBox.setChecked(true);
}else{
holder.checkBox.setChecked(false);
holder.checkBox.setClickable(true);
}
}
holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ExtraReserva extraReserva = new ExtraReserva(extra.getId(), "", -1, -1, 1, extra.isSuplemento());
Disponibilidad d = Disponibilidad.getInstance();
if(isChecked){
d.addExtra(extraReserva);
}
else d.deleteExtra(extraReserva.getIdExtra());
}
});
return view;
}
@Override
public int getViewType() {
return extra.getTipoPrecio();
}
private static class ViewHolder {
final TextView title;
final CheckBox checkBox;
final TextView total;
private ViewHolder(TextView title, CheckBox checkBox, TextView total) {
this.title = title;
this.checkBox = checkBox;
this.total = total;
}
}
}
Thank you for your help in advance.