guuuguel
guuuguel

Reputation: 23

Continous update firebase data on RecyclerView

When I add data, I want the data that I take to be updated instantly. I'm pulling the data out of the Firebase rooms, and I want it updated in RecyclerView when I add a new room. With the Getroom function, I'm pulling data from the Firebase and transferring it to RecyclerView. I'm adding rooms with AddRoom. When I add a room, I want the data in RecyclerView to be added Immediately. So I want to update by constantly pulling data from the Firebase

    public class RoomsActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
     private List<Rooms> roomList = new ArrayList<>();
        private RecyclerView recyclerView;
        private RoomsAdapter mAdapter;
        DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
        DatabaseReference ref = mDatabase.child("0").child("Rooms");
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
     recyclerView = (RecyclerView) findViewById(R.id.rvRooms);
              mAdapter = new RoomsAdapter(roomList);
              recyclerView.setAdapter(mAdapter); 
 getRoom();

        }

     public boolean onOptionsItemSelected(MenuItem item) {  if (id == R.id.add_rooms) {
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(RoomsActivity.this);
                LayoutInflater inflater = RoomsActivity.this.getLayoutInflater();
                final View dialogView = inflater.inflate(R.layout.rooms_dialog, null);
                dialogBuilder.setView(dialogView);

                final EditText edt = (EditText) dialogView.findViewById(R.id.edit1);

                dialogBuilder.setTitle("Oda Ekle");
                dialogBuilder.setMessage("Eklemek istediğiniz odanın adını yazınız");
                dialogBuilder.setPositiveButton("Ekle", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {
   final String roomsText =  edt.getText().toString();
   final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("0");               rootRef.child("Rooms").child(roomsText).setValue(roomsText);
                    }
                });
                dialogBuilder.setNegativeButton("İptal", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                });
                AlertDialog b = dialogBuilder.create();
                b.show();
            }
            return super.onOptionsItemSelected(item);
        }
 private void getRoom() {

  ref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
         for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                        String data = snapshot.getValue(String.class);
                        Rooms getdata = new Rooms(data);
                        if (!roomList.contains(getdata)) {
                            roomList.add(getdata);
                        }

                        mAdapter.notifyDataSetChanged();
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        }

Upvotes: 2

Views: 42

Answers (1)

ankur bhut
ankur bhut

Reputation: 125

You need to change your getroom() method event with the below method. And addListenerForSingleValueEvent method use for single time data retrieve. If you want continuous data then need to change your method.

ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    User bruceWayne = dataSnapshot.child("371298").getValue(User.class);
    // Do something with the retrieved data or Bruce Wayne
}

@Override
public void onCancelled(DatabaseError databaseError) {
    Log.e("UserListActivity", "Error occured");
    // Do something about the error
});

Upvotes: 1

Related Questions