1

Working on an Android Project using Firebase.

The project is to let users check in and display all logs in a table view.

I have set up the firebase database as follows:

Firebase image

I can't get the entries to show in the table view? I would like to show the following items per list item:

  • userCheckIn
  • username
  • userLocation

My code:

    private ListView mListView;
    private DatabaseReference mDatabse;
    private ArrayList<String> arrayList = new ArrayList<>();
    private ArrayAdapter<String> adapter;
    String Date;
    Calendar calendar;
    SimpleDateFormat simpleDateFormat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account);
        mListView = (ListView) findViewById(R.id.database_list_view);
        calendar = Calendar.getInstance();
        simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        Date = simpleDateFormat.format(calendar.getTime());

        FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser() ;
        mDatabse = FirebaseDatabase.getInstance().getReference().child("CheckInOut").child(currentFirebaseUser.getUid()).child(Date).child("userCheckIn");

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);

        mListView.setAdapter(adapter);

        mDatabse.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                String string = dataSnapshot.getValue(String.class);
                arrayList.add(string);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

I would appreciate any support possible.

Grimthorr
  • 6,856
  • 5
  • 41
  • 53
LionelF
  • 11
  • 1

1 Answers1

0

To solve this, move the declaration of your ArrayList and ArrayAdapter inside onChildAdded() method. You need also to set the adapter inside onChildAdded() method because this method has an asynchronous behaviour. This means that if you are trying to set the adapter outside onChildAdded() method it will always be empty because at that time you haven't got all the data from the database. So all operation must take place inside that method.

I recomand you also to use FirebaseUI. It will handle all the events for you. If want to read more about how you can get the data outside an asynchronous method, take a look at my answer from this post.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193