How to add a button to Firebase Recycler view in Android?

I want to add a button for each item in the Firebase recycler view. I've created the button, it's being showed, and i'm receiving the Toast when I press it. But - How can I get the relevant item on it's list ?

For example - When I press the 'DELETE' I want to delete that certain mission 'aspodm'. (Sorry for the large picture, how do I make it smaller?) ss

This is how the database looks like (vfPvsk... is the user id, and 773f... is random uuid for mission id):

dbref

And that is the code of the relevant fragment. (The Toast when clicking the button is activated - I just don't know how to get to the relevant mission in order to delete it)

 package com.airpal.yonatan.airpal;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;


/**
 * A simple {@link Fragment} subclass.
 */
public class PendingFragment_User extends Fragment {

    private String TAG = "dDEBUG";
    private RecyclerView mPendingList;

    private DatabaseReference mMissionsDb;
    private FirebaseAuth mAuth;

    private String mCurrent_user_id;

    private View mMainView;

    Query queries;

    public PendingFragment_User() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mMainView = inflater.inflate(R.layout.fragment_pending_user, container, false);

        mPendingList = (RecyclerView)mMainView.findViewById(R.id.pending_recycler_user);
        mAuth = FirebaseAuth.getInstance();

        mCurrent_user_id = mAuth.getCurrentUser().getUid();

        mMissionsDb = FirebaseDatabase.getInstance().getReference().child("Missions").child(mCurrent_user_id);
        queries = mMissionsDb.orderByChild("status").equalTo("Available");
        mMissionsDb.keepSynced(true);

        mPendingList.setHasFixedSize(true);
        mPendingList.setLayoutManager(new LinearLayoutManager(getContext()));


        // Inflate the layout for this fragment
        return mMainView;
    }

    @Override
    public void onStart() {
        super.onStart();

        FirebaseRecyclerAdapter<Mission, PendingFragment_User.MissionsViewHolder> firebaseMissionsUserRecyclerAdapter = new FirebaseRecyclerAdapter<Mission, PendingFragment_User.MissionsViewHolder>(
                Mission.class,
                R.layout.missions_single_layout,
                PendingFragment_User.MissionsViewHolder.class,
                queries
        ) {
            @Override
            protected void populateViewHolder(PendingFragment_User.MissionsViewHolder missionViewHolder, final Mission missionModel, int missionPosition) {
//                Log.d(TAG, "inside populateViewHolder" + missionModel.getType() + " , " + missionModel.getDescription());
                missionViewHolder.setMissionName(missionModel.getType());
                missionViewHolder.setMissionDescription(missionModel.getDescription());
                missionViewHolder.setMissionStatus(missionModel.getStatus());
                missionViewHolder.button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
//                        Toast.makeText(view.getContext(), "esto es un boton"+ view.getNextFocusUpId(), Toast.LENGTH_SHORT).show();
                        Log.d(TAG,"The button was pressed");
                    }
                });

            }
        };

        mPendingList.setAdapter(firebaseMissionsUserRecyclerAdapter);
    }

    public static class MissionsViewHolder extends RecyclerView.ViewHolder {
        View mView;
        Button button ;

        public MissionsViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            button = (Button)mView.findViewById(R.id.pending_single_button);

        }

        public void setMissionName(String name){
            TextView mMissionNameView = mView.findViewById(R.id.mission_single_name);
            mMissionNameView.setText(name);
        }

        public void setMissionStatus(String status){
            TextView mMissionStatusView = mView.findViewById(R.id.mission_single_status);
            mMissionStatusView.setText(status);
            if (status.equals("Available")){
                mMissionStatusView.setTextColor(Color.parseColor("#008000"));;
            } else {
                mMissionStatusView.setTextColor(Color.parseColor("#FF0000"));;
            }
        }

        public void setMissionDescription(String description){
            TextView mMissionDescriptionView = mView.findViewById(R.id.mission_single_description);
            mMissionDescriptionView.setText(description);

        }
    }
}
androidfirebasefirebase-databaseandroid-recyclerviewfirebaseui

Answers

answered 1 week ago pushasha #1

Try using a subclass rather than anonymous one. Then, you'll be able to access the getItem(int position) method of the adapter. Something along the lines of:

private class MissionAdapter extends FirebaseRecyclerAdapter<Mission, PendingFragment_User.MissionsViewHolder> {

    public MissionAdapter(Query queries){
        super(Mission.class, R.layout.missions_single_layout, PendingFragment_User.MissionsViewHolder.class, queries);
    }

    @Override
    protected void populateViewHolder(PendingFragment_User.MissionsViewHolder missionViewHolder, final Mission missionModel, final int missionPosition) {
        Log.d(TAG, "inside populateViewHolder" + missionModel.getType() + " , " + missionModel.getDescription());
        missionViewHolder.setMissionName(missionModel.getType());
        missionViewHolder.setMissionDescription(missionModel.getDescription());
        missionViewHolder.setMissionStatus(missionModel.getStatus());
        missionViewHolder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Mission clickedMission = MissionAdapter.this.getItem(missionPosition);
                if (clickedMission != null){ // for the sake of being extra-safe
                    Log.d(TAG,"The button was pressed for mission: " + clickedMission.getType());
                }
            }
        });
    }
}

I made it print a log, but you should be able to do anything you want with the retrieved Mission object in there.

comments powered by Disqus