问题
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
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" />
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;
}
}
回答1:
You posted these error messages in a comment:
No setter/field for -LxFfqThHtNWsk9bD5Zb
No setter/field for -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.
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.
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());
}
}
...
回答2:
Even I am a beginner Android developer so I think that you should have a separate activity whose instance should be called from this file.
来源:https://stackoverflow.com/questions/59517555/android-firebase-rtdb-retrieve-and-display-data