Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to get position in bindView like in getView?

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.

like image 844
lorraine Avatar asked Jan 28 '13 02:01

lorraine


2 Answers

Try this

public void bindView(View arg0, Context arg1, Cursor arg2)
{
    int pos = arg2.getPosition();
}
like image 184
subair_a Avatar answered Sep 27 '22 15:09

subair_a


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());
        }
    });
}
like image 27
EmCeSquare Avatar answered Sep 27 '22 16:09

EmCeSquare