L Thompson
L Thompson

Reputation: 171

Reading data from a Hashmap - Android studio, Firebase

Here is the image of my Firebase database setup:

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

Upvotes: 2

Views: 4830

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

To display all titles of all those objects in a ListView, please use the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("ProgressReports").child(uid);
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String title = ds.child("title").getValue(String.class);
            list.add(title);
            Log.d("TAG", title);
        }
        ListView listView = (ListView) findViewById(R.id.list_view);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
        listView.setAdapter(arrayAdapter);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(eventListener);

Your list will contain two titles:

Test
Test

More information here if you want to use a model class.

Upvotes: 2

Related Questions