Reputation: 899
I have List view with custom adapter and I have problem with removing items from list. Here is definition of list row
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:descendantFocusability="blocksDescendants">
<CheckBox
android:id="@+id/history_list_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"/>
<TextView
android:id="@+id/history_list_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/history_list_check"/>
<ImageView
android:id="@+id/history_list_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:contentDescription="@string/history_drunk_image_description"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
/>
</RelativeLayout>
My custom adapter:
public class HistoryArrayAdapter extends ArrayAdapter<Measure> {
private final List<HistoryRowModel> list;
private final Activity context;
public HistoryArrayAdapter(Activity context, List<Measure> list) {
super(context, R.layout.history_list_row, list);
this.context = context;
this.list = new ArrayList<HistoryRowModel>();
for(Measure measure : list) {
HistoryRowModel rowModel = new HistoryRowModel(measure);
this.list.add(rowModel);
}
}
static class ViewHolder {
protected CheckBox checkbox;
protected TextView text;
protected ImageView imageView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.history_list_row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.history_list_check);
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
HistoryRowModel element = (HistoryRowModel) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
}
});
viewHolder.text = (TextView) view.findViewById(R.id.history_list_label);
viewHolder.imageView = (ImageView) view.findViewById(R.id.history_list_image);
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.checkbox.setChecked(list.get(position).isSelected());
HistoryRowModel rowModel = list.get(position);
Measure measure = rowModel.getMeasure();
long time = measure.getTimestamp();
DateFormat dateFormat = SimpleDateFormat.getDateInstance();
holder.text.setText(dateFormat.format(time));
int resource = getImage(measure.getThreshold());
if(resource == -1)
{
holder.imageView.setImageResource(R.drawable.unknown);
}
else
{
holder.imageView.setImageResource(resource);
}
return view;
}
}
And here is my remove from list code:
private void removeSelectedRows() {
View v;
CheckBox cb;
for (int i = 0; i < list.getCount(); i++) {
v = list.getAdapter().getView(i, null, null);
cb = (CheckBox) v.findViewById(R.id.history_list_check);
if(cb.isChecked())
{
removeMeasure(i);
}
}
}
private void removeMeasure(long rowId) {
Measure toRemove = adapter.getItem((int)rowId);
try {
Dao<Measure, Integer> measureDao = databaseHelper.getMeasureDao();
DeleteBuilder<Measure, Integer> db = measureDao.deleteBuilder();
db.where().eq("id", toRemove.getId());
measureDao.delete(db.prepare());
}
catch(Exception e) {
e.printStackTrace();
}
adapter.remove(toRemove);
}
My problem is that when I check some item and then invoke removeSelectedRows its remove always last item from list. Then when I return from this activity and go back again it's turn out that the proper row was delete from database and list show proper list rows. So it's looks like line
adapter.remove(toRemove);
remove wrong item from adapter. Any idea what is wrong with this code?
njzk2 was right in his comment
Upvotes: 0
Views: 1892
Reputation: 2614
Replace all your adapter
calls with list.getAdapter()
calls. Or at least show us how you initialize the adapter
variable.
EDIT: The notifyOnChange flag is 'true' by default so calling any of the following adapter methods will invoke the notifyDataSetChanged() method of the same object: add(T), insert(T), remove(T), clear()
Upvotes: 1