Here is the image of my Firebase database setup:
I'm trying to program a simple notes application, I'm able to save everything to Firebase but struggling to retrieve the information.
A child of the user ID
is randomly generated which is causing me issues when trying to retrieve the data. The below code is how im saving all the data to the Firebase database.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress_reports);
findViewById(R.id.saveNoteButton).setOnClickListener(saveReport);
progressReportTitle = findViewById(R.id.progressReportTitle);
progressReportContent = findViewById(R.id.progressReportContent);
fAuth = FirebaseAuth.getInstance();
fProgressReportsDatabase = FirebaseDatabase.getInstance().getReference().child("ProgressReports").child(fAuth.getCurrentUser().getUid());
findViewById(R.id.loadNoteButton).setOnClickListener(loadReports);
}
View.OnClickListener saveReport = new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = progressReportTitle.getText().toString().trim();
String content = progressReportContent.getText().toString().trim();
if (title.isEmpty()) {
progressReportTitle.setError("Please Enter a title for this report");
progressReportTitle.requestFocus();
} else if (content.isEmpty()) {
progressReportContent.setError("No content within report");
progressReportContent.requestFocus();
} else {
saveReportToDB(title, content);
}
}
};
private void saveReportToDB(String title, String content) {
if (fAuth.getCurrentUser() != null) {
final DatabaseReference newProgressReportRef = fProgressReportsDatabase.push();
final Map progressReportMap = new HashMap();
progressReportMap.put("title", title);
progressReportMap.put("content", content);
progressReportMap.put("timestamp", ServerValue.TIMESTAMP);
And below is how im attempting to load this data into a listview on a separate activity, with no luck,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
mListView = (findViewById(R.id.progressList));
fAuth = FirebaseAuth.getInstance();
fFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = FirebaseDatabase.getInstance().getReference().child("ProgressReports").child(fAuth.getCurrentUser().getUid());;
FirebaseUser user = fAuth.getCurrentUser();
userID = user.getUid();
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void showData(DataSnapshot dataSnapshot){
for(DataSnapshot ds : dataSnapshot.getChildren()){
ReportInformation rInfo = new ReportInformation();
rInfo.setTitle(ds.child(userID).child("title").getValue(ReportInformation.class).getTitle());
rInfo.setContent(ds.child(userID).child("content").getValue(ReportInformation.class).getContent());
//rInfo.setTimestamp(ds.child(userID).child("timestamp").getValue().toString());
ArrayList<String> array = new ArrayList<>();
array.add(rInfo.getTitle());
array.add(rInfo.getContent());
//array.add(rInfo.getTimestamp());
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, array);
mListView.setAdapter(adapter);
}
}
What am I doing wrong, and how can I query the database for the HashMap
which is randomly generated?
Thanks guys