Ashutosh Patole
Ashutosh Patole

Reputation: 980

Fetch Data from firebase after clicking on a recyclerView

I have a recyclerview with a text which contain question. what i want to do is when a user clicks on the on of the question the expected answer should be shown in a textview in next view.

this is my database schema.

enter image description here

This is my home fragment.


import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.solver.widgets.Snapshot;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.firebase.ui.database.SnapshotParser;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;

import java.util.Objects;

public class HomeFragment extends Fragment {

    private RecyclerView recyclerView;
    FloatingActionButton addButton;
    DatabaseReference mDatabase;
    FirebaseRecyclerAdapter adapter;




    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View RootView = inflater.inflate(R.layout.fragment_home, container, false);
        addButton = RootView.findViewById(R.id.add);
        recyclerView = RootView.findViewById(R.id.my_recycler_view);

        mDatabase = FirebaseDatabase.getInstance().getReference("Question");
        mDatabase.keepSynced(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));

        Query query = FirebaseDatabase.getInstance().getReference().child("Question");

        FirebaseRecyclerOptions<Problem_Questions_RecyclerView> options = new FirebaseRecyclerOptions.Builder<Problem_Questions_RecyclerView>()
                .setQuery(query, new SnapshotParser<Problem_Questions_RecyclerView>() {
                    @NonNull
                    @Override
                    public Problem_Questions_RecyclerView parseSnapshot(@NonNull DataSnapshot snapshot) {

                        return new Problem_Questions_RecyclerView(Objects.requireNonNull(snapshot.child("ID").getValue()).toString(),
                                Objects.requireNonNull(snapshot.child("Question").getValue()).toString());
                    }
                }).build();
        adapter = new FirebaseRecyclerAdapter<Problem_Questions_RecyclerView, ViewHolder>(options){

            @NonNull
            @Override
            public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.questions_recycler_view, parent, false);
                return new ViewHolder(view);

            }

            @Override
            protected void onBindViewHolder(@NonNull ViewHolder viewHolder, int i, @NonNull Problem_Questions_RecyclerView details) {


                viewHolder.setQuestion(details.getQuestion());



                viewHolder.root.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                                new Answers()).addToBackStack(null).commit();

                    }
                });
            }
        };
        recyclerView.setAdapter(adapter);


        return RootView;
    }


    public class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout root;
        TextView Question;




        ViewHolder(@NonNull View itemView) {
            super(itemView);
            root = itemView.findViewById(R.id.list_root);
            Question = itemView.findViewById(R.id.txt);


        }

       void setQuestion(String string)
       {
           Question.setText(string);
       }


    }



    @Override
    public void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}



import android.app.DownloadManager;

public class Problem_Questions_RecyclerView {
    public Problem_Questions_RecyclerView(){

    }

    public void setUID(String UID) {
        this.UID = UID;
    }

    public String getQuestion() {
        return Question;
    }

    public void setLocation(String question) {
        Question = question;
    }



    public Problem_Questions_RecyclerView(String UID, String question) {
        this.UID = UID;
        Question = question;

    }

    private String UID,Question;


}




this is my answer class which contains the textview for showing answer. I have implemented something like when i click it should show the the question itself instead of answer(testing purpose), but i have to give constant ID for fetching data. what should i do to get the ID of the clicked recyclerview and show answer in next fragment

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class Answers extends Fragment {
    DatabaseReference mDatabase;
    private TextView textView;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.answers, container, false);


        mDatabase = FirebaseDatabase.getInstance().getReference().child("Question").child("xxyy").child("Question");



        textView = view.findViewById(R.id.answer);

        mDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {


                String v = dataSnapshot.getValue().toString();
                textView.setText(v);


            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });





        return view;

    }


}

Now, when i click on the recycler view having

question 2

it should open another fragment containing

this is answer 2


Upvotes: 0

Views: 66

Answers (1)

Denis95
Denis95

Reputation: 136

You need to do the following. In your model class, you should add another String containing the answer. When you have the answer stored inside your model class, in your adapter method onBindViewHolder, on the click method, do what you want to do, and pass the values you need in the new activity(you can parcel all the model class, or just pass the values you need, your choice).

Upvotes: 1

Related Questions