7uthaifah
7uthaifah

Reputation: 345

Getting Firebase children to RecyclerView

I'am using Firebase and I need to retrieve children to the RecyclerView not FirebaseRecyclerAdapter so I need to retrieve the children to the RecyclerView.

Here's my Firebase database structure:

"Following" : {
    "ORBBKfZAAUhqI1h7ojQDzYOjgkp1" : {
      "pypD1SYZkbcYesk09WuMUY1AkTf1" : {
        "UID" : "pypD1SYZkbcYesk09WuMUY1AkTf1"
      },
      "z2SNUlLd6mQM8nnlkU2VUws5Ggl2" : {
        "UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2"
      }
  },
  "Posts" : {
    "-KduqnVVczZf5uibQiZ-" : {
      "Describe" : "gg",
      "MostView" : -8,
      "Time" : 14881230655,
      "UID" : "pypD1SYZkbcYesk09WuMUY1AkTf1",
      "Username" : "Jone",
      "Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped906285501.jpg?alt=media&token=0c1a3a3d-6e48-4c4e-ba59-f5646bf8965f"
    },
    "-Ke5gJ00CxbjhOuhzLIx" : {
      "Describe" : "hajj",
      "MostView" : -9,
      "Time" : 1488318465,
      "UID" : "pypD1SYZkbcYesk09WuMUY1AkTf1",
      "Username" : "Dom",
      "Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1717103943.jpg?alt=media&token=a85b2488-5ac7-49a7-9ad0-dbf4e6f29389"
    },
    "-KeCuiFmUCpN19zwsTsR" : {
      "Describe" : "a",
      "MostView" : -2,
      "Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
      "Time" : 1488439652,
      "UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
      "Username" : "Dom",
      "Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1913886685.jpg?alt=media&token=c8ad26b2-8f09-453b-b48a-aad9e4d8b5c3"
    },
    "-KeD2fBUQ09HVMrvAneb" : {
      "Describe" : "ee",
      "MostView" : -2,
      "Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
      "Time" : 1488441999,
      "UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
      "Username" : "Dom",
      "Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1904338270.jpg?alt=media&token=d200b7fc-15e7-4dc4-9913-59c377929e9e"
    },
    "-KeDfnMWiB7k_4J3FBgZ" : {
      "Describe" : "f12g",
      "MostView" : -1,
      "Time" : 1488452517,
      "UID" : "ORBBKfZAAUhqI1h7ojQDzYOjgkp1" 
      "Username" : "Young",
      "Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1872832638.jpg?alt=media&token=80239844-a872-47e2-9689-92d368dd9452"
    },
    "-KeDfrw4inUQFtIDXJHp" : {
      "Describe" : "fg",
      "MostView" : -2,
      "Time" : 1488452536,
      "UID" : "ORBBKfZAAUhqI1h7ojQDzYOjgkp1" 
      "Username" : "Young",
      "Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1872832638.jpg?alt=media&token=80239844-a872-47e2-9689-92d368dd9452"
    },

  }
}

I need to show post for the following users only so I used this method:

    mPostList = (RecyclerView) v.findViewById(R.id.MainPostList);
    mPostList.setHasFixedSize(true);
    mPostList.setItemAnimator(new DefaultItemAnimator());
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    mPostList.setLayoutManager(layoutManager);

    mAuth = FirebaseAuth.getInstance();
    mCurrentUser = mAuth.getCurrentUser();

    mDatabase = FirebaseDatabase.getInstance().getReference().child("Posts");
    mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users");
    mDatabaseFriends = FirebaseDatabase.getInstance().getReference().child("Following").child(mCurrentUser.getUid());

    return v;
}

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

    mAuth.addAuthStateListener(mListener);

    mDatabaseFriends.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot2) {

            for (DataSnapshot GettingFollowingList : dataSnapshot2.getChildren()){
                mDatabase.orderByChild("UID").equalTo(GettingFollowingList.getKey()).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for (DataSnapshot Q : dataSnapshot.getChildren()){

                            Adapter MyAdapter = Q.getValue(Adapter.class);
                            mPostList.setAdapter(MyAdapter);}}
                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }});}}

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }});

And here's my Adapter:

class Adapter extends RecyclerView.Adapter<PostViewHolder> {
ArrayList<Getting_Posts> mdataSet;

public Adapter(ArrayList<Getting_Posts> dataSet){
    mdataSet = dataSet;
}

@Override
public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View inflatedView = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_card_design, parent, false);
    return new PostViewHolder(inflatedView);
}

@Override
public void onBindViewHolder(final PostViewHolder holder, int position) {
    Getting_Posts model = mdataSet.get(position);

    //model.getTime();
    //holder.mPostTime.setText("Q");
    // Set the properties of the holder
    // I don't know what properties you used in the viewholder so I'll just give an example
    // example: holder.Name.setText(post.getName());
}

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

public void add(int position, Getting_Posts post) {
    mdataSet.add(position, post);
    notifyItemInserted(position);
}

And here's my Getting Post:

public class Getting_Posts {

//Post
private Long Time;
private String Describe;
private String Wallpaper;
private String Username;
private String ProfilePic;

public Getting_Posts() {}

public Getting_Posts
        (Long Time,
         String Describe,
         String Wallpaper,
         String Username;
         String ProfilePic;
        ) {

    this.Time = Time;
    this.Describe = Describe;
    this.Wallpaper = Wallpaper;
    this.Username = Username;
    this.ProfilePic = ProfilePic;

}

public Long getTime() {
    return Time;
}

public void setTime(Long time) {
    Time = time;
}

public String getDescribe() {
    return Describe;
}

public void setDescribe(String describe) {
    Describe = describe;
}

public String getWallpaper() {
    return Wallpaper;
}

public void setWallpaper(String wallpaper) {
    Wallpaper = wallpaper;
}

public String getUsername() {
    return Username;
}

public void setUsername(String username) {
    Username = username;
}

public String getProfilePic() {
    return ProfilePic;
}

public void setProfilePic(String profilepic) {
    ProfilePic = profilepic;
}

but I'am getting this error:

com.google.firebase.database.DatabaseException: Class com.pcsoftgroup.test.fragments.Adapter is missing a constructor with no arguments
                                                                      at com.google.android.gms.internal.zzalq$zza.zze(Unknown Source)
                                                                      at com.google.android.gms.internal.zzalq$zza.zzcc(Unknown Source)
                                                                      at com.google.android.gms.internal.zzalq.zzd(Unknown Source)
                                                                      at com.google.android.gms.internal.zzalq.zzb(Unknown Source)
                                                                      at com.google.android.gms.internal.zzalq.zza(Unknown Source)
                                                                      at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                                      at com.pcsoftgroup.test.fragments.MainChallenges$3$1.onDataChange(MainChallenges.java:151)

Upvotes: 0

Views: 800

Answers (1)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363537

In general when you map the firebase data with a model class you have to add an empty constructor.

In your case you are mapping the data with the Adapter class

Adapter MyAdapter = Q.getValue(Adapter.class);

so you should add in this class the constructor.

class Adapter {

    public Adapter() {
          // Default constructor required for calls to DataSnapshot.getValue(Adapter.class)
    }

}

In your case it doesn't make sense.
You should cycle on the children (for example the Getting_Posts reference) then map each child with a Getting_Posts and the add them (a list of them) to the adapter.

Something like:

for (DataSnapshot Q : dataSnapshot.getChildren()){
              myList.add(Q.getValue(Getting_Posts.class);
}
myAdapter.setList(myList);

In any case you should try Firebase UI.
It is very simple to populate a RecyclerView with Firebase data.

Upvotes: 1

Related Questions