inahti
inahti

Reputation: 13

Show Firebase data in ListView

I'm having trouble showing stored data in ListView. Everything works well without errors but my data is not showing.

In my app a teacher registers and chooses the subjects they want to teach. The data is stored in Firebase database. On the next activity they are supposed to review their chosen subjects (the subjects are supposed to be shown in ListView). But, I just can't seem to get it to work. It shows up blank. I don't know what I'm doing wrong.

ConfirmSubjectsActivity


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.ArrayList;

public class ConfirmSubjectsActivity extends AppCompatActivity {

    private DatabaseReference mDatabase;
    private ListView list_id;
    private ArrayList<String> arrayList = new ArrayList<>();
    private ArrayAdapter<String> arrayAdapter;
    private FirebaseAuth mAuth;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_subjects);

        mAuth = FirebaseAuth.getInstance();
        FirebaseUser user = mAuth.getCurrentUser();
        String userId = user.getUid();

        arrayAdapter = new ArrayAdapter<String>(ConfirmSubjectsActivity.this, android.R.layout.simple_list_item_1, arrayList);
        list_id = findViewById(R.id.list_id);
        list_id.setAdapter(arrayAdapter);
        mDatabase = FirebaseDatabase.getInstance().getReference();


        mDatabase.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
                for (DataSnapshot subjectssnapshot: snapshot.getChildren()){
                    String value = subjectssnapshot.child("Predmeti").getValue(String.class);
                    arrayList.add(value);
                }
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot snapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }
}

"Predmeti" = "Subjects" just fyi

my database

Upvotes: 0

Views: 59

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

You may be correctly adding the data to the list, but until you notify the adapter of the changed data it won't repaint the UI.

To notify the adapter, call notifyDataSetChanged on it after you're done adding the items to the list:

mDatabase.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
        for (DataSnapshot subjectssnapshot: snapshot.getChildren()){
            String value = subjectssnapshot.child("Predmeti").getValue(String.class);
            arrayList.add(value);
        }
        arrayAdapter.notifyDataSetChanged();
    }

Also see the Android documentation on notifyDataSetChanged.

Upvotes: 2

Justin
Justin

Reputation: 299

You didn't add the child reference to your DB.

1.
Use This Line Instead
mDatabase = FirebaseDatabase.getInstance().getReference().child(User).child(Instruktor);

2.
getValue(String.class) will most probably retieve a list of true.
To refer the names within Predmeti you can try getKey()
String []value = subjectssnapshot.child("Predmeti").getValue(String.class);

3.
Use this to update the list
arrayAdapter.notifyDataSetChanged();

Upvotes: 1

Related Questions