I have a ListView with an Adapter extending the BaseAdapter and several items in my ListView. Every item has a CheckBox and this CheckBox can only be enabled and disabled from another Activity, so it is not possible to directly check the CheckBox in the ListView. However my problem is that if I check e.g. the third item, the first item is also checked. If I take a look into my database the first item should not be checked and if I debug this part, I do not see any reason why it should be checked. So what can be the reason that the first item gets checked as soon as I check another one? I heard that there is a problem with a ConvertView and CheckBoxes but I don't know how to solve this.
Let's go for some Code:
This is my Adapter.
public class ReportsAdapter extends BaseAdapter {
private List<Report> reports = new ArrayList<>();
private LayoutInflater inflater;
private Context context;
public ReportsAdapter(Context context, List<Report> reports) {
this.reports = reports;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
@Override
public int getCount() {
return reports.size();
}
@Override
public Report getItem(int position) {
return reports.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_item_layout, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Report currentReport = getItem(position);
try {
if (viewHolder.rowTvNr != null) {
viewHolder.rowTvNr.setText(String.valueOf(currentReport.getReportNr()));
}
if (viewHolder.rowTvWeekFrom != null) {
viewHolder.rowTvWeekFrom.setText(currentReport.getWeekFrom() + " ");
}
if (viewHolder.rowTvWeekTo != null) {
viewHolder.rowTvWeekTo.setText(currentReport.getWeekTo());
}
viewHolder.checkBox.setClickable(false);
if (viewHolder.checkBox != null && !viewHolder.rowTvNr.getText().equals("") || viewHolder.rowTvNr.getText() != null) {
viewHolder.checkBox.setChecked(currentReport.isSent());
} else {
viewHolder.checkBox.setChecked(false);
}
} catch (Exception e) {
Log.d("View Exception", "ListView Component is null" + e.toString());
}
return convertView;
}
private static class ViewHolder {
TextView rowTvNr, rowTvWeekFrom, rowTvWeekTo;
AnimateCheckBox checkBox;
public ViewHolder(View item) {
rowTvNr = (TextView) item.findViewById(R.id.rowTvWeekNr);
rowTvWeekFrom = (TextView) item.findViewById(R.id.rowWeekFrom);
rowTvWeekTo = (TextView) item.findViewById(R.id.rowWeekTo);
checkBox = (AnimateCheckBox) item.findViewById(R.id.dailyCheckBox);
}
}
}
Aucun commentaire:
Enregistrer un commentaire