Java – ListViewAdapter extends CursorAdapter Scroll when order confusion

ListViewAdapter extends CursorAdapter Scroll when order confusion… here is a solution to the problem.

ListViewAdapter extends CursorAdapter Scroll when order confusion

I’m confused. I found some suggestions online, but I couldn’t implement it on this code. This is my question. Every time I scroll, the order of the ListView gets confused. I don’t know how to do it. I really need some help. I will thank you very much for your kindness. Here is 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);

}
}

Solution

In Android, a View can be used

multiple times, which means that an instantiated View from a “newView” can be used more than once in “bindView”. To be clear: “newView” is not often called (<=) than “bind” View. Therefore, saving state in “newView” is not something you can do. In newView, you can only manipulate properties that evaluate all Views instantiated from the adapter that are not manipulated in bindView. All dynamic values for each single line (or item) are already set in the “bindView” because reuse of the View can (and will) occur. Saving a single internal View of a row (or item) in your adapter causes unexpected behavior and cannot be completed. This is your problem. NewView is called when there are no instantiated and idle (undisplayed/recyclable) Views in the “view-object-pool”. In addition, you must also consider resetting some subviews in the “bindView” in case a populated View appears here, and in special cases the properties of some rows/items remain unset. Finally: you have no way of knowing whether a given View in “bindView” is newly created or recycled. Hope for something clear. Happy coding

Related Problems and Solutions