Dennis_M
Dennis_M

Reputation: 360

Android - Recycling listview elements automatically?

I created a listview that has a custom SimpleCursorAdapter. I want to place a header in the first element in the list. 8 views fit on the screen at a time. When I scroll down to the ninth view, the header of the 1st element appears. At least I believe that is what is happening. I removed a button above the listview allowing all of the elements to appear on screen and only the first element had the header.

I believe I am forcing a new view to be inflated each time. I have read up a bit on convertview and it appears to be something that you have to implement manually.

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    final LayoutInflater inflater = LayoutInflater.from(context);
    int position = cursor.getPosition();
    View v;
    v = inflater.inflate(R.layout.roster_lv_row_entry_with_header, parent, false);      
    if(position > 0)
        v = inflater.inflate(R.layout.roster_lv_row_entry_no_header, parent, false);

    return v;

Upvotes: 2

Views: 2342

Answers (1)

rekaszeru
rekaszeru

Reputation: 19220

You shoul override the (final int position, View convertView, ViewGroup parent) method in your adapter class, and

  1. assign the convertView parameter a new value (if necessary, but better just use it if it's the right type, and fill it with the proper data based on yourListData.get(position), where yourListData is e.g. a List<?> extension.)
  2. then return it.

Upvotes: 4

Related Questions