Q:

sending row id when RecyclerView item clicked

I have prepared a Database in my asset folder . I have one recyclerView for showing the title in the database . Now  i want show DetailActivity with specificid . my means is show description of post . I think it can be done with intent , but i don't know how .
    public class FehrestRecyclerAdapter extends RecyclerView.Adapter<FehrestRecyclerAdapter.ViewHolder> {

    private Context ctx;
    private List<Post> posts;

    //constructor
    public FehrestRecyclerAdapter(Context ctx, List<Post> posts) {
        this.ctx = ctx;
        this.posts= posts;
    }

    //ViewHodler Pattern
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private TextView txtTitle;

        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);

            txtTitle= (TextView)itemView.findViewById(R.id.txtTitle);
        }

        @Override
        public void onClick(View v) {
            //Toast.makeText(v.getContext() , txtTitle.getText() , Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(itemView.getContext() , DetailActivity.class);
            intent.putExtra("postId" , "id");
            itemView.getContext().startActivity(intent);
        }
    }

    //OncreateViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v  = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_row , parent , false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    //OnBindViewHolder
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        Post current = Post.get(position);

        holder.txtTitle.setText(current.getTitle());
    }

    //getItemCount method
    @Override
    public int getItemCount() {
        return posts.size();
    }
}

i have a 3 columns in my database 

1.id 2.title 3.desc

and 3 methods in my Post model Class :

getId() 2.getTitle() 3.getDesc()

as you can see i can send intent to DetailActivity . But i don't know how can i achieve to sending specific id to DetailActivity. thanks

A:

Make one Variable id in ViewHolder and assign that in onBindViewHolder() , I updated the code according to that.
public class FehrestRecyclerAdapter extends RecyclerView.Adapter<FehrestRecyclerAdapter.ViewHolder> {

private Context ctx;
private List<Post> posts;

//constructor
public FehrestRecyclerAdapter(Context ctx, List<Post> posts) {
    this.ctx = ctx;
    this.posts= posts;
}

//ViewHodler Pattern
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    private TextView txtTitle;
    private int id;

    public ViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);

        txtTitle= (TextView)itemView.findViewById(R.id.txtTitle);
    }

    @Override
    public void onClick(View v) {
        //Toast.makeText(v.getContext() , txtTitle.getText() , Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(itemView.getContext() , DetailActivity.class);
        // here pass id through intent
        intent.putExtra("postId" , id);
        itemView.getContext().startActivity(intent);
    }
}

//OncreateViewHolder
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v  = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_row , parent , false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

//OnBindViewHolder
@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    Post current = Post.get(position);

    holder.txtTitle.setText(current.getTitle());

    // add this line
    holder.id = Post.get(postion).getId();

}

//getItemCount method
@Override
public int getItemCount() {
    return posts.size();
}
}

