【问题标题】:Firebase Android: Getting data stored in user Id then to the random Push().getKey() valueFirebase Android:获取存储在用户 ID 中的数据,然后获取随机 Push().getKey() 值
【发布时间】:2019-12-09 20:45:01
【问题描述】:

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

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?

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

你可以看到它首先有一个用户 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;
}

希望你能帮帮我

【问题讨论】:

    标签: java android database firebase


    【解决方案1】:

    这里不需要使用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) {
    
        }
    });
    

    【讨论】:

    • 非常感谢,这很有帮助,我想我必须得到每一层
    猜你喜欢
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 2017-07-06
    相关资源
    最近更新 更多