简体   繁体   English

Android - Firebase RTDB 检索和显示数据

[英]Android - Firebase RTDB retrieve and display data

Hello I want to retrieve data but I don't see anything get displayed and at the same time my app crashes when I don't have data in my database so at least it seems the connection works.你好,我想检索数据,但我没有看到任何显示,同时当我的数据库中没有数据时,我的应用程序崩溃,所以至少看起来连接有效。 I'm also a beginner in Android Studio.我也是 Android Studio 的初学者。

Note: This is a follow-up question from an earlier question .注意:这是来自较早问题的后续问题

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    Title = findViewById(R.id.tvNoteTitle);
    text = findViewById(R.id.TvNoteText);
    addNotePageButton = findViewById(R.id.fab_button_addPage);
    firebaseDatabase = FirebaseDatabase.getInstance();

    firebaseAuth = FirebaseAuth.getInstance(); // get inSTACE

    // acsess database and retrive data

    FirebaseDatabase.getInstance().getReference("NoteList").child(firebaseAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        NotesList notelist = dataSnapshot.getValue(NotesList.class);
        Title.setText( notelist.getTitle());
        text.setText(notelist.getText());
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Toast.makeText(HomeActivity.this,databaseError.getCode(),Toast.LENGTH_SHORT).show();
        }
    });
}

I wonder if all notes by a user will be displayed or only the first?我想知道是否会显示用户的所有笔记还是只显示第一个? I also wonder if I can add a delete button to every note without using view holders or anything else我还想知道是否可以在不使用视图持有者或其他任何东西的情况下为每个笔记添加删除按钮

Here is my XML for that view the relative layout这是我用于该视图的 XML 相对布局

<TextView
    android:id="@+id/tvNoteTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="64dp"
    android:text="Title: "
    android:textSize="28dp"

    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="parent" />

<TextView
    android:id="@+id/TvNoteText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="356dp"
    android:text="Text: "
    android:textSize="22dp"

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tvNoteTitle"
    app:layout_constraintVertical_bias="0.653" />

my database strucutre;:我的数据库结构;:

火力数据库结构

public class NotesList {

    private String title;
    private String text;

    public NotesList(){

    }

    public NotesList(String title, String text){

        this.title=title;
        this.text= text;

    }

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

即使我是一个初学者 Android 开发者,所以我认为你应该有一个单独的活动,它的实例应该从这个文件中调用。

You posted these error messages in a comment:您在评论中发布了这些错误消息:

No setter/field for -LxFfqThHtNWsk9bD5Zb -LxFfqThHtNWsk9bD5Zb 没有设置器/字段

No setter/field for -LxFfqThHtNWsk9bD5Zb -LxFfqThHtNWsk9bD5Zb 没有设置器/字段

This typically means that you're trying to parse the wrong level in your JSON data.这通常意味着您试图解析 JSON 数据中的错误级别。 You're trying to parse a node that has children -LxFfqThHtNWsk9bD5Zb and -LxFfqThHtNWsk9bD5Zb into an object that doesn't have properties with that name.您正在尝试将具有子节点-LxFfqThHtNWsk9bD5Zb-LxFfqThHtNWsk9bD5Zb的节点解析为不具有该名称属性的对象。

Most likely you have a list of notes for each user under /NoteList/$uid , which means you also need to handle that list in your onDataChange by iterating over the children of the snapshot you get.很可能您在/NoteList/$uid下有每个用户的笔记列表,这意味着您还需要通过迭代您获得的快照的子项来处理您的onDataChange中的列表。 Something like this should do the trick:像这样的事情应该可以解决问题:

FirebaseDatabase.getInstance().getReference("NoteList").child(firebaseAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    for (DataSnapshot noteSnapshot: dataSnapshot.getChildren()) {
      System.out.println("Reading note from "+noteSnapshot.getKey());

      NotesList notelist = noteSnapshot.getValue(NotesList.class);

      System.out.println(notelist);
    }
  }
  ...

If you run this, you can see that it loads the notes from your database.如果你运行它,你可以看到它从你的数据库中加载了笔记。


To display a list of notes in the app, you'll want to use a Recycler View.要在应用程序中显示笔记列表,您需要使用 Recycler 视图。 But I highly recommend reading a tutorial on how to do that , rather than repeat that here.但我强烈建议阅读有关如何做到这一点教程,而不是在这里重复。 Alternatively, you can use the adapters in FirebaseUI to populate your recycler view, rather than building your own adapter.或者,您可以使用FirebaseUI 中的适配器来填充您的回收器视图,而不是构建您自己的适配器。

With your current code and layout you can set a single note's title and text into the UI with:使用您当前的代码和布局,您可以使用以下命令将单个笔记的标题和文本设置到 UI 中:

FirebaseDatabase.getInstance().getReference("NoteList").child(firebaseAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    for (DataSnapshot noteSnapshot: dataSnapshot.getChildren()) {
      System.out.println("Reading note from "+noteSnapshot.getKey());

      NotesList notelist = noteSnapshot.getValue(NotesList.class);

      Title.setText(notelist.getTitle());
      text.setText(notelist.getText());
    }
  }
  ...

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

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