2

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;
Dennis_M
  • 340
  • 1
  • 4
  • 10

1 Answers1

4

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.
rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • I am having an issue with this. Where my app is loading an image in the background.. I have three items visible in the view and when I click on one of them. they repaint... the problem I am having is. that the listview is reusing the items that are still on the screen. or so it appears. – The Lazy Coder Apr 20 '11 at 20:49
  • 1
    To avoid that, you should ALWAYS assign a new value to the convertView parameter, as it's in the 1. step. This way it won't cache and scrolling won't cause this side-effect. – rekaszeru Apr 20 '11 at 20:55