I am so confused. I've found some suggestions on the net but I can't implement it on this code. Here's my problem. Everytime I scroll, list view's order messes up. I have no idea what to do. I really need some help. I will really appreciate your kindness. Here's my code:
public class ListViewAdapterMeasurement extends CursorAdapter {
TextView lblContent, lblDate;
DBHelper dbHelper;
Button btnSelect;
public ListViewAdapterMeasurement(Context context, Cursor c) {
super(context, c, FLAG_REGISTER_CONTENT_OBSERVER);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.details_feed, parent, false);
lblContent = (TextView) view.findViewById(R.id.lblContent);
lblDate = (TextView) view.findViewById(R.id.lblDate);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
View convertView = view;
if (convertView == null)
{
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.details_feed, null);
}
dbHelper = new DBHelper(getApplicationContext());
int intContentIndex = cursor.getColumnIndex((Tables.FeedTable.COLUMN_CONTENT));
String strContentIndex = cursor.getString(intContentIndex);
int intDateIndex = cursor.getColumnIndex((Tables.FeedTable.COLUMN_DATE));
String strDateIndex = cursor.getString(intDateIndex);
lblContent.setText(strContentIndex);
lblDate.setText(strDateIndex);
}
}
In Android the views can be used several times, which means an already instantiated view from "newView" can be used in "bindView" more than once.To be clear: "newView" is called not so often(<=) than "bindView". Saving states in "newView" therefor is not a thing you can do. In "newView" you can only manipulate properties which counts for all views ever instantiated from this adapter which are not manipulated in "bindView". All dynamic values for each single row( or item) has do be set in "bindView" since there a reused view can(and will) appear. Saving single inner views of the row (or item) in your adapter leads to unexpected behavior and can not be done. This is your problem. "newView" is called when there is no already instantiated and free(not shown/ recyclable) view in the "view-object-pool". Also you must consider to reset certain subviews in "bindView" in the case an already filled view comes along here and a property stays unset for certain row/item in special circumstances. At the end: You can not know if in "bindView" the given view is a newly constructed or recycled one. Hope that clear things. Happy coding