1

I have to load data from the cursor to listview in my app. Here is my custom adapter class. And it works fine. But I need to show a separate layout for very first row of the listview. The first row in list will have an extra imageview. How can I do so? Please help me. Thanks in advance.

public class CustomSCAdapter extends SimpleCursorAdapter {
Cursor c;
Context context;
Activity activity;
int layout;

public CustomSCAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags ) {
    super(context, layout, c, from, to, flags);
    this.c = c;
    this.context=context;
    this.activity=(Activity) context;
    this.layout = layout;

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    final View view = LayoutInflater.from(context).inflate(this.layout, parent, false);
    return view;
}  

@Override
public void bindView(View view, Context context, Cursor cursor) {
       //here finding all my views in row from view object and binding data from cursor as per my requirement.
     }

}

EDIT : Actually i'am passing null for cursor variable for constructor of adapter class as i'am using loaders mechanism in my fragment.

    getLoaderManager().initLoader(0, null, this);
    adapter = new CustomSCAdapter(getActivity(), R.layout.row_layout, null, from, to, 0);
Santhosh
  • 4,956
  • 12
  • 62
  • 90
  • don't override newView. there is no need. (means you don't need to keep a reference to the layout, the activity, the context, and the cursor. SimpleCursorAdapter does all that for you). in bindView, cursor.getPosition() gives you the position. if it is 0, display your image, else hide it. – njzk2 Jun 10 '13 at 11:39
  • but if i scroll down the list view, getposition() returns 0 again – Santhosh Jun 10 '13 at 11:42
  • getPosition returns 0 when the cursor adapter is binding the first item of the cursor. as Nachi pointed out, isFirst works as well and is prettier. – njzk2 Jun 10 '13 at 11:46
  • i don't see the relation with the update in your question. (except keeping a reference to c when you know c is null and the actual cursor of the adapter will chance is really a bad idea) – njzk2 Jun 10 '13 at 12:11

1 Answers1

0

The easiest way would be to treat the first item as a header to the list as documented in this answer.

A simpler (but uglier) approach would be to put these clauses in your newView and bindView methods:

if (cursor.isFirst()) {
 // inflate a different layout containing the imageview or populate the imageview
} else {
 // add logic for other rows here
}
Community
  • 1
  • 1
Nachi
  • 4,218
  • 2
  • 37
  • 58
  • 1
    i am not sure of the behaviour of list view if you inflate 2 different layouts, as the recycling is handled in the simplecursoradapter. I'd say you need to override getItemViewType as well to tell simplecursoradapter 2 different layouts are used. – njzk2 Jun 10 '13 at 11:45
  • I cannot use instance cursor variable as its value is null always.(question is updated) and if I use isFirst method on cursor parameter of bindView method to identify the first item, it returns true for multiple rows on every scroll down as adapter binds data to visible views only. – Santhosh Jun 10 '13 at 12:02