1

I create 2 android apps, for the user and for admin. The user app can crud the "pelanggaran", the admin app can see all the pelanggaran realtime. Here, I am stuck in retrieving the data in real time for the admin app. Here's the database hierarchy.

this is the references: pelanggaran and user

each user has pelanggaran. so, the first child of pelanggaran is userID, then the pelanggaranID, then the values

I tried googling, I got it, but it is not real-time. Here's my code

private void refreshList(){
    databaseUser = FirebaseDatabase.getInstance().getReference("User");
    pelanggaranList = new ArrayList<>();
    databaseUser.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            pelanggaranList.clear();
            long numberUsers = dataSnapshot.getChildrenCount();
            Log.w("total user", "" + numberUsers);

            for (DataSnapshot dsUser : dataSnapshot.getChildren()){
                User user = dsUser.getValue(User.class);
                final String userId = user.getUserId();
                Log.w("id user", userId);

                databasePelanggaran = FirebaseDatabase.getInstance().getReference("Pelanggaran").child(userId);
                databasePelanggaran.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot1) {
                        for (DataSnapshot dsPelanggaran : dataSnapshot1.getChildren()){
                            Pelanggaran pelanggaran = dsPelanggaran.getValue(Pelanggaran.class);
                            Log.w("id pelanggaran", userId + " : " + pelanggaran.getIdPelanggaran());
                            pelanggaranList.add(pelanggaran);
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
            PelanggaranAdapter pelanggaranAdapter = new PelanggaranAdapter(MainActivity.this, pelanggaranList);
            recyclerView.setAdapter(pelanggaranAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

It can take all the values from each user but it is not real time. And sometimes the data doesn't appears. What should I do to make it realtime?

Jaymin
  • 2,879
  • 3
  • 19
  • 35
  • If you are interested, **[this](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is how you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo May 14 '18 at 08:50
  • Thank you @AlexMamo, I have already made it with RecyclerView. Now, I am confused of how to retrieve the values of Pelanggaran in each pelanggaranId in each userId – Ivana Natalia May 14 '18 at 09:49
  • on list item click? – Alex Mamo May 14 '18 at 10:02
  • I made it when the parent layout (Relative Layout) is clicked, and I made it in RecyclerViewHolder class. The problem is the values doesn't appear in the admin app real time. When I updated, or added a Pelanggaran in user app, the Pelanggaran in admin app doesn't update and if I refresh, the datas don' appear anymore @AlexMamo – Ivana Natalia May 14 '18 at 14:31

0 Answers0