Skip to content
Advertisement

Show data from Firebase in Recyclerview, filtered on username – How to?

With the following line of code I’m able to show the data under “results” of the User “Hendrik”.

Query query = FirebaseDatabase.getInstance().getReference("users/Hendrik/results");

But what I want, is to show the data under “results” according to which user is logged in. I think I’m supposed to work with FirebaseAuth to check which user is currently logged in? I’m not really sure tho, anyone has any suggestions?

Database tree

enter image description here

Inside my fragment

private void setUpRecyclerView() {
        Query query = FirebaseDatabase.getInstance().getReference("users/Hendrik/results");

        FirebaseRecyclerOptions<Results> options = new FirebaseRecyclerOptions.Builder<Results>()
                .setQuery(query, Results.class)
                .build();

        adapter = new ResultAdapter(options);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (adapter != null) {
            adapter.startListening();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (adapter != null) {
            adapter.stopListening();
        }
    }

My adapter

public class ResultAdapter extends FirebaseRecyclerAdapter<Results, ResultAdapter.ResultViewHolder> {

public ResultAdapter(@NonNull FirebaseRecyclerOptions<Results> options){
    super(options);
}

@Override
public void onBindViewHolder(@NonNull ResultViewHolder holder, int position, @NonNull Results model) {

    holder.datum.setText(model.getDatum());
}

@NonNull
@Override
public ResultViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.result_display_layout, parent, false);
    return new ResultViewHolder(v);
}


public static class ResultViewHolder extends RecyclerView.ViewHolder{

    TextView datum;
    ImageView resultaat1, resultaat2, resultaat3, resultaat4;
    public ResultViewHolder(@NonNull View itemView){
        super(itemView);

        datum = itemView.findViewById(R.id.datum);
        resultaat1 = itemView.findViewById(R.id.schietResultaat1);
        resultaat2 = itemView.findViewById(R.id.schietResultaat2);
        resultaat3 = itemView.findViewById(R.id.schietResultaat3);
        resultaat4 = itemView.findViewById(R.id.schietResultaat4);
    }
}
}

Advertisement

Answer

I think I’m supposed to work with FirebaseAuth to check which user is currently logged in?

Yes. FirebaseAuth will generate unique ids for every user then you can structure your data like Users->uid-> then details

After that you could do this:

But what I want, is to show the data under “results” according to which user is logged in

by get uid of the current logged in user from FirebaseAuth and your database query will look like this:

Query query = FirebaseDatabase.getInstance().getReference().child("users").child(uid).child("result");
Advertisement