gpt4 book ai didi

java - Firebase Android : Getting data stored in user Id then to the random Push(). getKey() 值

转载 作者:行者123 更新时间:2023-11-29 22:38:45 24 4
gpt4 key购买 nike

我的上一个问题已关闭并链接到其他一些相同的问题,但没有一个涵盖我的情况,我仍然需要一些帮助。他们链接到的帖子是:

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?

然而,我仍在挣扎,希望有人能帮助我。

enter image description here

如您所见,它首先有一个用户 ID,然后有随 secret 钥,然后是我想要获得的两个信息。当我尝试获取信息时,我的应用程序一直崩溃

我目前正在做的代码:onCreate

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


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

之后:

  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) {

}
});
}

savedQuotes 类具有以下信息:

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;
}

希望你能帮帮我

最佳答案

这里不需要使用databaseReference.push().getKey()。检查以下内容:

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) {

}
});

关于java - Firebase Android : Getting data stored in user Id then to the random Push(). getKey() 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59256317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com