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

时间:2019-12-09 20:45:01

标签: java android database firebase

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

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,然后具有随机密钥,然后是我想要获取的两个信息。当我尝试获取信息时,我的应用程序不断崩溃

我目前正在执行的代码: 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;
}

希望您能为我提供帮助

1 个答案:

答案 0 :(得分:1)

此处无需使用 // First create a button style that gets the isEnabled value injected struct MyButtonStyle: ButtonStyle { private let isEnabled: Bool init(isEnabled: Bool = true) { self.isEnabled = isEnabled } func makeBody(configuration: Configuration) -> some View { return configuration .label .background(isEnabled ? .green : .gray) .foregroundColor(isEnabled ? .black : .white) } } // Then make a ViewModifier to inject the state struct MyButtonModifier: ViewModifier { @Environment(\.isEnabled) var isEnabled func body(content: Content) -> some View { return content.buttonStyle(MyButtonStyle(isEnabled: isEnabled)) } } // Then create a convenience function to apply the modifier extension Button { func styled() -> some View { ModifiedContent(content: self, modifier: MyButtonModifier()) } } // Finally, try out the button and watch it respond to it's state struct ContentView: View { var body: some View { Button("Test", {}).styled().disabled(true) } } 。检查以下内容:

databaseReference.push().getKey()