Android-Firebase RTDB检索和显示数据 [英] Android - Firebase RTDB retrieve and display data

查看:78
本文介绍了Android-Firebase RTDB检索和显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想检索数据,但没有看到任何显示,同时,当我的数据库中没有数据时,我的应用程序崩溃,因此至少看起来连接可以正常工作.我也是Android Studio的初学者.

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.

注意:这是来自一个先前的问题的后续问题.

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

这是该视图相对布局的XML

Here is my XML for that view the relative layout

<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" />

我的数据库结构;

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

推荐答案

您在评论中张贴了这些错误消息:

You posted these error messages in a comment:

-LxFfqThHtNWsk9bD5Zb没有设置器/字段

No setter/field for -LxFfqThHtNWsk9bD5Zb

-LxFfqThHtNWsk9bD5Zb没有设置器/字段

No setter/field for -LxFfqThHtNWsk9bD5Zb

这通常意味着您正在尝试解析JSON数据中的错误级别.您正在尝试将具有子级-LxFfqThHtNWsk9bD5Zb-LxFfqThHtNWsk9bD5Zb的节点解析为不具有该名称的属性的对象.

This typically means that you're trying to parse the wrong level in your JSON data. You're trying to parse a node that has children -LxFfqThHtNWsk9bD5Zb and -LxFfqThHtNWsk9bD5Zb into an object that doesn't have properties with that name.

您很有可能在/NoteList/$uid下为每个用户都有一个列表注释,这意味着您还需要遍历所获取快照的子代来处理onDataChange中的该列表. .这样的事情应该可以解决问题:

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. 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.

要在应用程序中显示笔记列表,您需要使用回收站视图".但我强烈建议您阅读有关如何执行该操作的教程,而不是在这里重复.或者,您可以使用 FirebaseUI 中的适配器来填充您的回收站视图,而不是构建自己的回收站视图适配器.

To display a list of notes in the app, you'll want to use a Recycler View. 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.

使用当前代码和布局,您可以通过以下方式在界面中设置单个便笺的标题和文本:

With your current code and layout you can set a single note's title and text into the UI with:

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());
    }
  }
  ...

这篇关于Android-Firebase RTDB检索和显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆