0

This is how I structured the my data

enter image description here

I can get the current user - curtUser, and the id for each of the gig posts. I've tried using different methods like .child("users").child(curtUser).child("Gig posts") but I'm getting a lot of errors that states the viewholder cannot display the elements of desserts which is the name, description, firstletter and ammount.

            final String curtUserId;
            DatabaseReference mDatabaseGig, databaseGig;
            FirebaseAuth mFirebaseAuth;
            FirebaseDatabase mFirebaseDatabase;

            mFirebaseAuth= FirebaseAuth.getInstance();
            mFirebaseDatabase = FirebaseDatabase.getInstance();
            FirebaseUser currUser = mFirebaseAuth.getCurrentUser();
            curtUserId = currUser.getUid();

            mDatabaseGig = mFirebaseDatabase.getReference("/users/" + curtUserId + "/Gig posts/");
            final String id = mDatabaseGig.push().getKey();
            final List<Dessert> dessertList;

            dessertList = new ArrayList<>();
           final DessertAdapter adapter = new DessertAdapter(getContext(), dessertList);
            recyclerView.setAdapter(adapter);

            mDatabaseGig.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    dessertList.clear();
                    for(DataSnapshot gigSnapshot: dataSnapshot.getChildren()){
                        Users user = new Users();
                        Dessert dessert = dataSnapshot
                                .child(id)
                                .getValue(Dessert.class);
                        dessertList.add(dessert);
                        adapter.notifyDataSetChanged();
                    }
                }
KENdi
  • 7,576
  • 2
  • 16
  • 31

1 Answers1

0

To get those values, please use the following code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference gigPostsRef = rootRef.child("users").child(uid).child("Gig posts");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String amount = ds.child("amount").getValue(String.class);
            String description = ds.child("description").getValue(String.class);
            String firstLetter = ds.child("firstLetter").getValue(String.class);
            String name = ds.child("name").getValue(String.class);
            Log.d("TAG", amount + " / " + description + " / " + firstLetter + " / " + name);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
gigPostsRef.addListenerForSingleValueEvent(eventListener);

Your output will be:

40 / is gone / M /mai
10 / i need someone to take notes for me / T / TAKE NOTES
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • THANKS. I'm trying to "get" the values on a string, I'm trying to save them to as an object then display it on a recycler view . – user3267274 Dec 24 '17 at 12:55
  • If you have a model class, the you can get the data as an object of the class. If not, use the data to create a new object of the class and use it in your RecylerView. – Alex Mamo Dec 24 '17 at 12:57
  • here is my current error 'Attempt to invoke virtual method 'java.lang.String com.example.gig.Model.Dessert.getName()' on a null object reference at com.example.gig.Adapter.DessertAdapter.onBindViewHolder(DessertAdapter.java:65) at com.example.gig.Adapter.DessertAdapter.onBindViewHolder(DessertAdapter.java:21)' – user3267274 Dec 26 '17 at 10:36
  • here is the line for DessertaAdapter.java.65 holder.mName.setText(dessert.getName()); and for line 2= public class DessertAdapter extends RecyclerView.Adapter – user3267274 Dec 26 '17 at 10:41
  • I do have a model class called Dessert. Here is my implementation of getting the data as an object of the class 'public void onDataChange(DataSnapshot dataSnapshot) { dessertList.clear(); for(DataSnapshot gigSnapshot: dataSnapshot.getChildren()){ Dessert dessert = gigSnapshot //.child(id) .getValue(Dessert.class); dessertList.add(dessert); adapter.notifyDataSetChanged();' – user3267274 Dec 26 '17 at 10:44
  • 'Attempt to invoke virtual method 'java.lang.String com.example.gig.Model.Dessert.getName()' on a null object reference means that your object is `null`. Check that object to return the correct value and not null. Does my above code work? – Alex Mamo Dec 27 '17 at 07:12
  • It works, thanks. But I need the data to be saved as a whole object that has name, amount, description elements - which I already created i.e the desserts object, not as separate string entities. I have debugged it and I think something is wrong with my adapter class. Here's the link to the question I posted about it https://stackoverflow.com/questions/47978270/null-pointer-exception-on-null-object-reference – user3267274 Dec 29 '17 at 00:00