1

I am new to android. I am trying to create a RecyclerView using this tutorial. The RecyclerView works fine. My problem is, I want to get the title and description of the clicked item of the RecyclerView.

Can someone help me? Here is my full code.

Updated

    public class View_Holder extends RecyclerView.ViewHolder implements View.OnClickListener{

        CardView cv;
        TextView title;
        TextView description;
        ImageView imageView;


        View_Holder(View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.cardView);
            title = (TextView) itemView.findViewById(R.id.title);
            description = (TextView) itemView.findViewById(R.id.description);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);

            //added by me
            title.setOnClickListener(this);
            description.setOnClickListener(this);
            imageView.setOnClickListener(this);
        }

        //added by me

        public ImageView getImageView() {
            return imageView;
        }
        public TextView getTitle() {
            return title;
        }
        public TextView getDescription() {
            return description;
        }

        @Override
        public void onClick(View v) {
           // Toast.makeText(get, "CLICK", Toast.LENGTH_SHORT).show();
            Log.i("positon-of-clicked-item", String.valueOf(getAdapterPosition()));
            Log.i("positon-of-clicked-item", String.valueOf(getLayoutPosition()));
            Log.i("positon-of-clicked-item", String.valueOf(this.getTitle()));
            Log.i("positon-of-clicked-item", String.valueOf(this.getDescription()));
           // getLayoutPosition(); //no method like that
        }
Sathya Baman
  • 2,867
  • 7
  • 35
  • 70

3 Answers3

3

Just use the ViewHolder View Objects to get details like below.

@Override
public void onClick(View v) {
   // Toast.makeText(get, "CLICK", Toast.LENGTH_SHORT).show();
    Log.i("positon-of-clicked-item", String.valueOf(getAdapterPosition()));
    Log.i("positon-of-clicked-item", String.valueOf(getLayoutPosition()));
    Log.i("positon-of-clicked-item", String.valueOf(title.getText());
    Log.i("positon-of-clicked-item", String.valueOf(sescription.getText()));
   // getLayoutPosition(); //no method like that
}
Mahendra Chhimwal
  • 1,792
  • 5
  • 18
  • 32
  • Hy. Tnx this is what i was Expecting. – Sathya Baman Mar 06 '16 at 08:20
  • you are welcome. By the way you should try bind data item to ViewHolder for better handling click and states for RecyclerView Items. http://stackoverflow.com/questions/35265865/recyclerview-causes-issue-when-recycling/35266327#35266327 – Mahendra Chhimwal Mar 06 '16 at 08:49
1

Call getAdapterPosition() inside onClick(), then with the position get item from the list you are populating.

Update :

Easiest way is that you put your ViewHolder inside your adapter class. Then you can access List<Data> of your adapter.

Sayem
  • 4,271
  • 2
  • 25
  • 42
  • hy tnx , am able get the clicked position. but can you help me to get the title for the clicked position also. Tried but don't work. i have updated the question with the code. – Sathya Baman Mar 06 '16 at 06:20
1

There's a couple good related posts/writeups on this at the below links:

The solution is easy to code but not something that becomes immediately apparent if you're new to using the RecyclerView.

In your View_Holder class, you need to do 2 things:

  1. In the constructor for the view holder, pass a reference to your adapter(or whatever you want to respond to the click) and store it. There are examples of this in the above links.
  2. Override the onClick method to respond to the click event for the selected item. Then in the onClick method, make a call to the getAdapterPosition() method to retrieve the position of the current item in the cursor used to power the adapter. Then call a method you define in the adapter class you passed a reference to in step 1 and pass the needed position of the cursor to that method as a parameter.

In the method that you call from the onClick event, you'll need to look up the information you want from the cursor.

Community
  • 1
  • 1
mabeechen
  • 121
  • 5