gpt4 book ai didi

java - Android - Firebase RTDB 检索和显示数据

转载 作者:行者123 更新时间:2023-12-02 09:09:43 25 4
gpt4 key购买 nike

您好,我想检索数据,但我没有看到任何显示,同时当我的数据库中没有数据时,我的应用程序崩溃了,所以至少看起来连接有效。我也是 Android Studio 的初学者。

注意:这是 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();
}
});
}

我想知道是否会显示用户的所有注释还是仅显示第一个?我还想知道是否可以在不使用 View 持有者或其他任何东西的情况下为每个笔记添加删除按钮

这是我的 XML View 的相对布局

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

我的数据库结构;:

firebase database structure

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

最佳答案

您在评论中发布了这些错误消息:

No setter/field for -LxFfqThHtNWsk9bD5Zb

No setter/field for -LxFfqThHtNWsk9bD5Zb

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

很可能您在 /NoteList/$uid 下有每个用户的注释列表,这意味着您还需要在 onDataChange 中处理该列表 通过迭代您获得的快照的子项。像这样的事情应该可以解决问题:

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

如果运行此命令,您可以看到它从数据库加载了注释。

<小时/>

要在应用程序中显示笔记列表,您需要使用回收器 View 。但我强烈建议阅读 tutorial on how to do that ,而不是在这里重复。或者,您可以使用 FirebaseUI 中的适配器。填充您的回收器 View ,而不是构建您自己的适配器。

使用当前的代码和布局,您可以通过以下方式将单个注释的标题和文本设置到 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());
}
}
...

关于java - Android - Firebase RTDB 检索和显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59517555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com