1

My Previous question was closed and was linked to some other same questions but non of them cover my case and I still require some help. The post they were linked to were:

Android: Retrieve data of a specific user from a Firebase database

Retrieving data from Firebase Realtime Database in Android

Retrieve Current User's Data (user_id) Firebase?

However, I am still struggling and hope someone can help me.

enter image description here

As you can see that it has an User ID first then has the random key then the two information i want to get. My application keeps crashing when i try to get the information

My code in what i am currently doing: onCreate

auth = FirebaseAuth.getInstance();
    user = auth.getCurrentUser();


    databaseReference = FirebaseDatabase.getInstance().getReference();

After that:

  protected void onStart() {
    super.onStart();
    String userID = user.getUid();
    databaseReference.child(userID).child(databaseReference.push().getKey()).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot data : dataSnapshot.getChildren()){
                Author author = data.getValue(Author.class);
                authorList.add(author);
            }
            SavedQuotes savedQuotes = new SavedQuotes(ShowQuotes.this, authorList);
            list.setAdapter(savedQuotes);
        }

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

        }
    });
}

The savedQuotes class has this information:

public SavedQuotes(Activity activity, List<Author> authorQuotes){
    super(activity, R.layout.activity_saved_quotes, authorQuotes);
    this.activity = activity;
    this.authors = authorQuotes;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater inflater = activity.getLayoutInflater();
    View listView = inflater.inflate(R.layout.activity_saved_quotes, null, true);

    name = listView.findViewById(R.id.name);
    quotes = listView.findViewById(R.id.quote);

    Author author = authors.get(position);
    name.setText(author.getName());
    quotes.setText(author.getQuote());

    return listView;
}

I hope you can help me please

AHPV
  • 77
  • 7

1 Answers1

1

No need to use databaseReference.push().getKey() here. Check below:

databaseReference = FirebaseDatabase.getInstance().getReference();

databaseReference.child(userID).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot data : dataSnapshot.getChildren()){
            Author author = data.getValue(Author.class);
            authorList.add(author);
        }

        ...
    }

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

    }
});
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46