19

In getView() of CursorAdapter, there's a parameter position so I can do a checking for that position, how can I do the same on bindView() it has no position parameter in BindView.

Currently I'm overriding newView(), bindView() and getView() which I read and heard is bad, either override getView() OR newView() and getView().

Thanks.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
lorraine batol
  • 6,001
  • 16
  • 55
  • 114
  • 2
    I am not sure your last statement is correct. I think it should read "Either override `getView` or (`newView` and `bindView`)". There is a default implementation of `getView`, that calls `newView` if there isn't a view yet, and `bindView` if the view is being recycled. – CoatedMoose Mar 21 '13 at 07:15

3 Answers3

36

Try this

public void bindView(View arg0, Context arg1, Cursor arg2)
{
    int pos = arg2.getPosition();
}
brenjt
  • 15,997
  • 13
  • 77
  • 118
subair_a
  • 1,028
  • 2
  • 14
  • 19
  • 3
    yeah I think this is it. But why is the cursor.getPosition() in bindView differs from the result of doing the cursor.getPosition() in newView. More on that here: http://stackoverflow.com/questions/14557251/android-bindview-and-newview-for-two-view-layout-in-cursoradapter – lorraine batol Jan 28 '13 at 07:20
5

codeboys answer is correct, cursor.getPosition() will do. But if someone needs position in the onClick event of listitems subitem, for instance icon inside the listItem, the solution is to put a position as setTag on the icon and retreive it when event occurs:

@Override
public void bindView(View vw, Context ctx, final Cursor cursor) {
    /* ...
    *  do your binding 
    */
    ImageView icon = (ImageView) vw.findViewById(R.id.your_icon);
    icon.setTag(cursor.getPosition());   // here you set position on a tag
    icon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //  use a tag to get the right position 
            ((MainActivity) context).onIconClick((int)v.getTag());
        }
    });
}
EmCeSquare
  • 79
  • 1
  • 4
  • There is no need to use "Tag" here because Java supports closures. You can use `pos` captured from the outer context inside your click listener if it is defined inside `bindView` as it is in your example – SergGr Mar 01 '17 at 17:31
0
@Override
public void bindView(View view, Context context, Cursor cursor) {
    // TODO Auto-generated method stub

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup view) {
    // TODO Auto-generated method stub
    int mCount = cursor.getCount();
    //Returns Total count(Rows) in cursor

     int currentPostion= cursor.getPosition();
    //Returns the current position of the cursor in the row set
}
edwin
  • 7,985
  • 10
  • 51
  • 82
  • 1
    how do i know what view I'm dealing with in bindView if I'm having two layouts in newView. please see http://stackoverflow.com/questions/14557251/android-bindview-and-newview-for-two-view-layout-in-cursoradapter – lorraine batol Jan 28 '13 at 07:54
  • 4
    This will not work. ListView recycles rows, so newView only gets called for a subset of the total rows you can scroll view. If you used `getPosition()` in bindView you would effectively track the currently drawn position. – CoatedMoose Mar 21 '13 at 07:16
  • @CoatedMoose Then how would you do it? – theblang Nov 08 '13 at 18:46
  • @mattblang as I said in my previous comment, the default implementation doesn't call `newView` on every list item because it re-uses list items. A reused list item gets called in `bindView`, so you need to use `cursor.getPosition()` in both `bindView` and `newView`. – CoatedMoose Nov 17 '13 at 21:14