使用CursorLoader时,在RecyclerView项目中单击光标

时间:2016-07-22 16:18:35

标签: android android-recyclerview android-cursoradapter android-cursorloader

您好我正在使用this库来实现带有CursorLoader的RecyclerView。它工作正常,但我想要的是在点击RecyclerView项目时获取光标。

如果是ListView,我可以通过

轻松获取光标
Cursor cursor = (Cursor) listView.getAdapter.getItem(position); 
ListView Itemclick中的

;

但我无法对RecyclerView做同样的事情。怎么做?

我尝试了什么:

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();
                }
            }
        });

    }
  }
}

2 个答案:

答案 0 :(得分:1)

适配器具有受保护的成员变量mCursor。您可以使用它来使用

检索预期的光标
if (mCursor != null) {
    mCursor.moveToPosition(position)
    //TODO: handle your functionality here (i.e. mCursor now points to the correct item)
}

然而,该特定库提供的CursorAdapter更像是一个hack而不是真正的解决方案。我建议使用更完整的库,例如RecyclerExt来控制所有内容,而不是试图将实际功能移交给ListView CursorAdapter

答案 1 :(得分:1)

以下是我按照@RocketSpock的回答修复它的方法。

@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());
    holder.view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCursorAdapter.getCursor().moveToPosition(position);
            Toast.makeText(mContext, "SSSSS "+mCursorAdapter.getCursor()
                    .getString(NAME_INDEX),Toast.LENGTH_SHORT).show();
        }
    });

}

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

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

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

    @Override
    public void bindCursor(Cursor cursor) {
        mMovieName.setText(mCursorAdapter.getCursor().getString(NAME_INDEX));

    }

}