繁体   English   中英

Firebase Android:获取存储在用户 ID 中的数据,然后获取随机 Push().getKey() 值

[英]Firebase Android: Getting data stored in user Id then to the random Push().getKey() value

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

Android:从 Firebase 数据库中检索特定用户的数据

在 Android 中从 Firebase 实时数据库中检索数据

检索当前用户的数据 (user_id) Firebase?

但是,我仍然在挣扎,希望有人可以帮助我。

在此处输入图片说明

正如您所看到的,它首先有一个用户 ID,然后有随机密钥,然后是我想要获得的两个信息。 当我尝试获取信息时,我的应用程序不断崩溃

我目前正在做的代码: 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) {

    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM