添加Firebase数据时加载两次数据

时间:2019-07-04 07:34:08

标签: android firebase

我正在从Firebase中提取数据并将数据添加到数据库中。这个过程顺利进行。但是,当我添加一个房间时,数据再次上传到RecyclerView,这意味着有两个相同的RecyclerView。添加数据后,我只希望添加新数据,这样就不会再次加载旧数据。只是想将新数据添加到旧数据的底部。

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");

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 recyclerView = (RecyclerView) findViewById(R.id.rvRooms);
          mAdapter = new RoomsAdapter(roomList);
          recyclerView.setAdapter(mAdapter);

            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(linearLayoutManager);

            getRoom();
}
 @Override
    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").addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {

                            dataSnapshot.getRef().child(roomsText).setValue(roomsText);

                        }
                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                        }
                    });

                }
            });
            dialogBuilder.setNegativeButton("İptal", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    //pass
                }
            });
            AlertDialog b = dialogBuilder.create();
            b.show();

        }

        return super.onOptionsItemSelected(item);
    }
}

private void getRoom() {

        ref.addValueEventListener(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);
                    roomList.add(getdata);

                    mAdapter.notifyDataSetChanged();


                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

                Toast.makeText(getApplicationContext(), "AWW SNAP... Something is Wrong.", Toast.LENGTH_LONG).show();
            }
        });

2 个答案:

答案 0 :(得分:0)

将您的方法更改为以下内容,并使用包含ArrayList的方法进行检查

ref.addValueEventListener(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) {

        Toast.makeText(getApplicationContext(), "AWW SNAP... Something is Wrong.", Toast.LENGTH_LONG).show();
    }
});

答案 1 :(得分:0)

只需清除列表,然后再添加recyclerview即可,就像这样:-

private void getRoom(){

    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

             if(roomList.getSize() != 0){
                  roomList.clear(); 
              }

            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                String data = snapshot.getValue(String.class);

                Rooms getdata = new Rooms(data);
                roomList.add(getdata);

                mAdapter.notifyDataSetChanged();


            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

            Toast.makeText(getApplicationContext(), "AWW SNAP... Something is Wrong.", Toast.LENGTH_LONG).show();
        }
    });