7

I am able to detect the individual recycler view item position and able to toast it on clicked. Now I wan't to move on to a new activity and show the details of the item clicked, how can I do that? Say, I am displaying Contact names list and onclick I wan't to open a new activity show that contact's details... At least, how can I toast the contact name again on clicking on that contact item, how are the current variables on clicking available to me? I am planning to bundle those variables and send them with intent and display there.

i know i have to implement here

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {  
private OnItemClickListener mListener;

public interface OnItemClickListener {
    public void onItemClick(View view, int position){
      //i know i have to implement here 

    }


}
Dolle Tulsi
  • 81
  • 1
  • 4
  • you should use Intent and putParcceable Extra in your Intent Like `putExtra("details",List[positon]);` – Syed Qasim Ahmed Nov 23 '15 at 13:01
  • create model class which can store all your fields, have an arraylist of model class, fill the arraylist while setting adapter , when you need data just get arraylist's object using the position you get in your `onitemclick`. – karan Nov 23 '15 at 13:03
  • post your adapter code – Pavan Nov 23 '15 at 13:05

4 Answers4

1

Lets take your own example of contact list to explain you the complete concept. Suppose we have a custom class called Contact which contains a String "Name" as shown below :-

class Contact implements Parcelable{
    String name;
    public void setName(String name) {
        this.name = name;
    }
    public String getName() { return name; }
    // Add your Parcelable code here
}

Now, inside your activity where you have the listener attached to your recyclerview, your code would be like the following :-

List<Contact> contacts = new ArrayList<>();
// You have to initialize your contacts list with data, I just gave you an example
recyclerItemClickListener = new RecyclerItemClickListener(this,
            new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                    intent.putExtra("Key", contacts.get(position);
                    startActivity(intent);
                }
            });
recyclerView.addOnItemTouchListener(recyclerItemClickListener);

Updated answer :-

To use the values of key in another activty do this :-

Contact contact = getIntent().getParcelableExtra("Key");

The above line of code retrieves the Contact object from the intent by help of your key.

Varun Kumar
  • 1,241
  • 1
  • 9
  • 18
1

I had the same problemm untill i did this.

Created a custome RecyclerListener:

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    }

    GestureDetector mGestureDetector;

    public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
        View childView = view.findChildViewUnder(e.getX(), e.getY());
        if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
            mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
}

then in the activity with the recyclerView:

private void registerCallClickBack() {

        recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity().getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(this, DetailActivity.class);
                intent.putExtra("contact_name", customList.get(position).getName());
                intent.putExtra("contact_image", customList.get(position).getImage());
                intent.putExtra("contact_tel", customList.get(position).getMobile());
                intent.putExtra("contact_email", customList.get(position).getEmail());
                startActivity(intent);
            }
        }));
    }

where customList is my ArrayList of contacts.

Hope it helps

Kostas Drak
  • 3,222
  • 6
  • 28
  • 60
0

You have to implement onClickListener for View in the RecyclerView.Adapter's holder class first Like this and start activity from there with data accessed through holder.

public class ViewHolder extends RecyclerView.ViewHolder  implements View.OnClickListener
    {
        TextView  textView1 , textView2;
        public ViewHolder(View v) {
            super(v);
            textView1= (TextView) v.findViewById(R.id.textview1);
            textView2= (TextView) v.findViewById(R.id.textview2);
            v.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            ViewHolder h = (ViewHolder)v.getTag();
            Intent next = new Intent(YourClass.this, ClassToOpen.class);
            next.putExtra("text1", h.textView1.getText().toString());
            next.putExtra("text2", h.textView2.getText().toString());
            startActivity(next);

        }


    }

After this in your onCreateViewHolder you have to set tag to your view like this.

    @Override
    public YourAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rowView = LayoutInflater.from(getActivity()).inflate(R.layout.row , parent , false);
        ViewHolder holder = new ViewHolder(rowView);
        rowView.setTag(holder);
        return holder;
    }

Other implementations should be like whatever you want.

Arslan Ashraf
  • 867
  • 7
  • 12
0

I had the same problem.

1st approach is attach all the required data with intent as extras and then start the required activity with that intent. Inside the new activity, get all the extras from the intent. This approach works for small amount of data, but not good if you have to pass large number of data to another activity.

2nd approach which i used is as follow.

Create a model class which stores all your required data fields. Make an arraylist of your model class, and populate your recyclerview using this arraylist. Now each item of your arraylist is an object of model class.

suppose

your model class is MyModel.java

your arraylist is ArrayList<'MyModel> myArrayList

Now make a globel reference of your model class. e.g

MyModel model;

make setter and getter methods for the above reference.

Now, since each item of your recyclerview is an object of model class. when you click on an item, inside onClick method, set above global reference to the clicked item(object of model class) using setter method. Then start new activity (from inside onClick method)using intent, and inside onCreate of newly created activity, get access to the global refernce. In this way you can get access to the clicked item(model object) and get required fields(data) out of that object.

Muhammad Ibrahim
  • 289
  • 2
  • 12