Search code examples
androidfirebase-realtime-databaseandroid-listview

How can I display data from database in my listview one by one?


I am new in developing apps. I try to create reservation system.

I would like to retrieve data and display on listview as shown below;

here

name should be under the textview for name and phone for phone etc..

public class Resden extends AppCompatActivity {
    DatabaseReference depref;
    FirebaseDatabase fdata;
    TextView namev, phonev, emailv, arrv, depv, paxv, ratev, roomtypev;
    Guest newguest;

    ListView listView;
    private ArrayAdapter<String> arrayAdapter;
    private ArrayList<String> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resden);
        listView = (ListView) findViewById(R.id.lviewy);
        list = new ArrayList<>();
        arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
        fdata = FirebaseDatabase.getInstance();
        depref = fdata.getReference("Guests");
        listView.setAdapter(arrayAdapter);


        depref.addChildEventListener(new ChildEventListener() {
                                         @Override
                                         public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                                             for(DataSnapshot childSnapShot : dataSnapshot.getChildren()) {
                                                 String name = (String) childSnapShot.child("Guests").getValue();
                                                 String phone = (String) childSnapShot.child("phone").getValue();
                                                 String email = (String) childSnapShot.child("email").getValue();
                                                 String arriving = (String) childSnapShot.child("arriving").getValue();
                                                 String departure = (String) childSnapShot.child("departure").getValue();
                                                 String pax = (String) childSnapShot.child("pax").getValue();
                                                 String rate = (String) childSnapShot.child("rate").getValue();
                                                 String roomtype = (String) childSnapShot.child("roomtype").getValue();

                                                 list.add(name);
                                                 list.add(phone);
                                                 list.add(email);
                                                 list.add(arriving);
                                                 list.add(departure);
                                                 list.add(pax);
                                                 list.add(rate);
                                                 list.add(roomtype);



                                             }

                                         }

and this is my activity

I also created Guest class with getter and setter..

THANKS IN ADVANCE


Solution

  • You need to move the arrayAdapter inside onChildAdded.

    public class Resden extends AppCompatActivity {
    DatabaseReference depref;
    FirebaseDatabase fdata;
    TextView namev, phonev, emailv, arrv, depv, paxv, ratev, roomtypev;
    Guest newguest;
    
    ListView listView;
    private ArrayAdapter<String> arrayAdapter;
    private ArrayList<String> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resden);
        listView = (ListView) findViewById(R.id.lviewy);
    
        fdata = FirebaseDatabase.getInstance();
        depref = fdata.getReference("Guests");
    
    
        depref.addChildEventListener(new ChildEventListener() {
                                         @Override
                                         public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
    
                                             for(DataSnapshot childSnapShot : dataSnapshot.getChildren()) {
                                                 String name = (String) childSnapShot.child("Guests").getValue();
                                                 String phone = (String) childSnapShot.child("phone").getValue();
                                                 String email = (String) childSnapShot.child("email").getValue();
                                                 String arriving = (String) childSnapShot.child("arriving").getValue();
                                                 String departure = (String) childSnapShot.child("departure").getValue();
                                                 String pax = (String) childSnapShot.child("pax").getValue();
                                                 String rate = (String) childSnapShot.child("rate").getValue();
                                                 String roomtype = (String) childSnapShot.child("roomtype").getValue();
    
                                                 list.add(name);
                                                 list.add(phone);
                                                 list.add(email);
                                                 list.add(arriving);
                                                 list.add(departure);
                                                 list.add(pax);
                                                 list.add(rate);
                                                 list.add(roomtype);
    
    
    
                                             }
    
                                                arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
                                                listView.setAdapter(arrayAdapter);
    
                                         }
    

    .addChildEventListener(listener) works async so will be always null if you fill the adapter outside of the listener.