【发布时间】:2020-04-18 09:58:58
【问题描述】:
您好,我想检索数据,但我没有看到任何显示,同时当我的数据库中没有数据时,我的应用程序崩溃了,所以至少看起来连接有效。我也是 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();
}
});
}
我想知道是显示用户的所有笔记还是只显示第一个?我还想知道是否可以在不使用视图持有者或其他任何东西的情况下为每个笔记添加一个删除按钮
这是我的 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" />
我的数据库结构;:
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 似乎是我的问题,那是我在特定用户上的帖子的 id,所以可以,但我的班级中有 setter?跨度>
-
请将您的数据库结构添加为 JSON 文件或至少一个屏幕截图和您的
NotesList类的内容。 -
我在原帖里加了。
标签: java android firebase firebase-realtime-database