Search code examples
androidandroid-recyclerviewandroid-cursoradapterandroid-cursorloader

Getting cursor in RecyclerView item click when using CursorLoader


Hi I am using this library to implement RecyclerView with CursorLoader. It is working correctly but what I want is getting cursor when Clicking RecyclerView item.

If it was ListView I could easily get the cursor by

Cursor cursor = (Cursor) listView.getAdapter.getItem(position); 

in ListView Itemclick;

But I'm not able to do the same with RecyclerView. How to do this?

What I've tried:

public class MovieAdapter extends RecyclerViewCursorAdapter<MovieAdapter.MovieViewHolder>{

/**
 * Column projection for the query to pull Movies from the database.
 */
public static final String[] MOVIE_COLUMNS = new String[] {
        MovieContract.MovieEntry.TABLE_NAME + "." + MovieContract.MovieEntry._ID,
        MovieContract.MovieEntry.COLUMN_NAME
};

/**
 * Index of the name column.
 */
private static final int NAME_INDEX = 1;

/**
 * Constructor.
 * @param context The Context the Adapter is displayed in.
 */
public MovieAdapter(Context context) {
    super(context);

    setupCursorAdapter(null, 0, R.layout.list_item_movie, false);
}

/**
 * Returns the ViewHolder to use for this adapter.
 */
@Override
public MovieViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new MovieViewHolder(mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent));
}

/**
 * Moves the Cursor of the CursorAdapter to the appropriate position and binds the view for
 * that item.
 */
@Override
public void onBindViewHolder(MovieViewHolder holder, int position) {
    // Move cursor to this position
    mCursorAdapter.getCursor().moveToPosition(position);

    // Set the ViewHolder
    setViewHolder(holder);

    // Bind this view
    mCursorAdapter.bindView(null, mContext, mCursorAdapter.getCursor());
}

/**
 * ViewHolder used to display a movie name.
 */
public class MovieViewHolder extends RecyclerViewCursorViewHolder {
    public final TextView mMovieName;

    public MovieViewHolder(View view) {
        super(view);

        mMovieName = (TextView) view.findViewById(R.id.movie_name);

    }

    @Override
    public void bindCursor(Cursor cursor) {
        mMovieName.setText(cursor.getString(NAME_INDEX));
        mMovieName.setTag(mCursorAdapter.getCursor());
        mMovieName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(v.getTag()!=null){
                    Cursor cursorTag = (Cursor)v.getTag();

                    **//Here I get last item's name in every item click using both cursorTag and  cursor.**
                    Toast.makeText(mContext, "SSSSS "+cursor.getString(NAME_INDEX),Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
  }
}

Solution

  • The adapter has a protected member variable mCursor. You can use this to retrieve the expected cursor with

    if (mCursor != null) {
        mCursor.moveToPosition(position)
        //TODO: handle your functionality here (i.e. mCursor now points to the correct item)
    }
    

    However that particular library provides a CursorAdapter that is more of a hack than a real solution. I recommend using a more complete library such as RecyclerExt that controls everything instead of attempting to hand off the real functionality to the ListView CursorAdapter