foarkipeju
foarkipeju

Reputation: 45

RecyclerView passing information to activity

I have an application with recyclerview and firebase.

In my Activity I can choose to display images that are in my firebase database and storage. I can choose to display all images, or I can choose to display images from a specific DATE.

If I want to display all images i use this code:

mDatabaseRef = FirebaseDatabase.getInstance().getReference();

If I want to display images from a specific date, I name the child to the date when the image was stored in the database like this:

mDatabaseRef = FirebaseDatabase.getInstance().getReference().child("2018-03.01");

Now I would like to make this dynamic, based on what DATE I click in my recyclerview, the CHILD needs to change to that string/date.

So if I click on the date 2013-03-01 I want this "string" to be passed to the activity which I am intending to open.

My user interface is currently showing the dates when the image was stored in firebase e.g:

2018-02-27

2018-02-28

2018-03-01

So when I press for example 2018-02-27 I want to pass this date to my activity and use it as the name of the child in firebase!

Here is my adapter:

@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
    // Upload is my Upload class with constructor, setter, and getter.
    // mUploads is my List


    Upload dateId= mUploads.get(position);

    // The String "dateNameId" gets all the dates,
    // 2018-02-27    2018-02-28    2018-03-01

    String dateNameId = dateId.getId();

    holder.textViewName.setText(dateNameId);


}

Now here is the code in the activity that actually sets the Id that we retrieve in our Adapter:

mDatabaseRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {

                Upload upload = postSnapshot.getValue(Upload.class);

                //The setId that takes the Key in firebase which is the date
                upload.setId(postSnapshot.getKey());

                mUploads.add(upload);

            }

I do not know how to get just the date that I am clicking on in my recyclerview

Upvotes: 1

Views: 121

Answers (2)

foarkipeju
foarkipeju

Reputation: 45

I solved it!

On the onItemClick I can use the position and the getId method to get the id of the adapter that is clicked. And since my Id is the date this solved my problem.

@Override
public void onItemClick(int position) {
    Upload selectedItem = mUploads.get(position);
    String selectedId = selectedItem.getId();
    Intent intent = new Intent(this , ImagesActivity.class);
    intent.putExtra("selectedId", selectedId);

    startActivity(intent);
}

Upvotes: 0

Akshay
Akshay

Reputation: 318

Add this class in your Activity Class`

class RecyclerTouchListener implements RecyclerView.OnItemTouchListener{
    private ClickListener clicklistener;
    private GestureDetector gestureDetector;
    public RecyclerTouchListener (Context context, final RecyclerView recycleView, final ClickListener clicklistener){
        this.clicklistener=clicklistener;
        gestureDetector=new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }
            @Override
            public void onLongPress(MotionEvent e) {
                View child=recycleView.findChildViewUnder(e.getX(),e.getY());
                if(child!=null && clicklistener!=null){
                    clicklistener.onLongClick(child,recycleView.getChildAdapterPosition(child));
                }
            }
        });
    }
    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        View child=rv.findChildViewUnder(e.getX(),e.getY());
        if(child!=null && clicklistener!=null && gestureDetector.onTouchEvent(e)){
            clicklistener.onClick(child,rv.getChildAdapterPosition(child));
        }
        return false;
    }
    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {}
    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {}
}

Create a Interface for click listener methods`

public static interface ClickListener{
    public void onClick (View view, int position);
    public void onLongClick (View view, int position);
}`

Implement the Recyclerview Onclicklistener

recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, recyclerView, new ClickListener() {
            @Override
            public void onClick (View view, final int position) {
                Toast.makeText(Activity.this, "press on position :" + position, Toast.LENGTH_LONG).show();
                //do something
            }
            @Override
            public void onLongClick (View view, int position) {
                Toast.makeText(Activity.this, "Long press on position :" position, Toast.LENGTH_LONG).show();
       //do something
            }
        }));

Finally you will get adapter position, in onclick event use the position to get the element from the Arraylist in the same Activity, there is no overhead of passing data between adapter and Activity.

Upvotes: 1

Related Questions