0

I'm trying to retrieve some data from firebase in a recyclerview. The issue is that it displays only the last object added to the database. I try to display the data from a fragment and then I have the layout " viewteamsfragment" which contains only a recyclerview and the layout of a row of it. This is how the database look like. enter image description here

The fragment which displays.

package com.projectfinalwannabe;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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.Query;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class View_TeamsFragment extends Fragment {

    RecyclerView teams_recyclerView;
    private DatabaseReference teams_dbRef;
    private ArrayList<Team> teamsList;
    private FedTeamsListAdapter fedTeamsListAdapter;

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

        teams_recyclerView = root.findViewById(R.id.teams2_recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        teams_recyclerView.setLayoutManager(linearLayoutManager);
        teams_recyclerView.setHasFixedSize(true);

        teams_dbRef = FirebaseDatabase.getInstance().getReference();
        teamsList = new ArrayList<>();

        clearData();
        getData();

        fedTeamsListAdapter = new FedTeamsListAdapter(getContext(), teamsList);
        teams_recyclerView.setAdapter(fedTeamsListAdapter);
        fedTeamsListAdapter.notifyDataSetChanged();

        return root;
    }

    private void getData() {

        Query query = teams_dbRef.child("Echipa");

        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                clearData();

                for(DataSnapshot dataSnapshot : snapshot.getChildren()){
                    Team team = new Team();

                    team.setName(dataSnapshot.child("Numele echipei").getValue().toString());
                    team.setCategory(dataSnapshot.child("Categoria de varsta").getValue().toString());

                    teamsList.add(team);
                }

                fedTeamsListAdapter = new FedTeamsListAdapter(getContext(), teamsList);
                teams_recyclerView.setAdapter(fedTeamsListAdapter);
                fedTeamsListAdapter.notifyDataSetChanged();
            }

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

            }
        });
    }

    private void clearData() {
        if(teamsList!=null){
            teamsList.clear();

            if(fedTeamsListAdapter!=null){
                fedTeamsListAdapter.notifyDataSetChanged();
            }
        }
        teamsList = new ArrayList<>();
    }
}

The Adapter

package com.projectfinalwannabe;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class FedTeamsListAdapter extends RecyclerView.Adapter<FedTeamsListAdapter.ViewHolder> {

    private Context context;
    private ArrayList<Team> teamsList;
    public FedTeamsListAdapter(Context context, ArrayList<Team> teamsList) {
        this.context = context;
        this.teamsList = teamsList;
    }

    @NonNull
    @Override
    public FedTeamsListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.team_layout, parent, false);
        return new FedTeamsListAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FedTeamsListAdapter.ViewHolder holder, int position) {
        holder.teamname_tv.setText(teamsList.get(position).getName());
        holder.agecategory_tv.setText(teamsList.get(position).getCategory());
    }

    @Override
    public int getItemCount() {
        return teamsList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView teamname_tv, agecategory_tv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            teamname_tv = itemView.findViewById(R.id.teamName_tv);
            agecategory_tv = itemView.findViewById(R.id.teamCategory_tv);
        }
    }
}

And the model class

import androidx.annotation.Keep;

import java.io.Serializable;

@Keep
public class Team implements Serializable {

    public static String name;
    public static String location;
    public static String category;

    public Team(){}

    public Team(String name, String location, String category){
        this.name = name;
        this.location = location;
        this.category = category;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public static String getName() {
        return name;
    }

    public static String getLocation() {
        return location;
    }

    public static String getCategory() {
        return category;
    }
}

And the layout of the team row

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardElevation="2dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    <RelativeLayout
        android:id="@+id/team_RL"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical"
        android:padding="10dp">

        <TextView
            android:id="@+id/teamName_tv"
            android:layout_width="290dp"
            android:layout_height="wrap_content"
            android:text="@string/numele_echipei"
            android:textSize="30sp"
            android:textStyle="normal" />

        <TextView
            android:id="@+id/teamCategory_tv"
            android:layout_width="290dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/teamName_tv"
            android:text="@string/teamCity"
            android:textSize="20sp"
            android:textStyle="italic" />

        <Button
            android:id="@+id/team_details"
            android:layout_width="95dp"
            android:layout_height="50dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop="10dp"
            android:text="@string/details"/>

    </RelativeLayout>

    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

2 Answers2

0

in your fragment

 private void getData() {

        Query query = teams_dbRef.child("Echipa");

        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                clearData();

                for(DataSnapshot dataSnapshot : snapshot.getChildren()){
                   Team team = dataSnapshot.getValue(Team.class)
                    
                    teamsList.add(team);
                }

                fedTeamsListAdapter = new FedTeamsListAdapter(YourActivity.this, teamsList);
                teams_recyclerView.setAdapter(fedTeamsListAdapter);
                fedTeamsListAdapter.notifyDataSetChanged();
            }

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

            }
        });
    }
Elango
  • 406
  • 3
  • 11
0

You see only one item because single row will cover entire screen if you set height as match_parent.so in your team row layout, change layout height to wrap_content.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"

    android:layout_height="wrap_content">
androidLearner
  • 1,654
  • 1
  • 7
  • 13
  • Thank u! Now it works fine. The next problem is that is showing only the last child but as many object were added in the database. Like, I have 3 teams added and it shows me the last team 3 times. – AmaliaVoicea Jun 05 '21 at 21:04
  • 1
    I figured it out... it was because my variables and getters from the model class were static. It might help somebody else ... – AmaliaVoicea Jun 10 '21 at 10:04