Reputation: 31
When I add a post, the list repeats again, I would like add just one post .
No problem when writing data on Firebase but there is a problem when reading from Firebase, all posts repeats ..
final NotesAdapter notesAdapter=new NotesAdapter(list,this);
recyclerView.setAdapter(notesAdapter);
FloatingActionButton fab = findViewById(R.id.fab);
firebaseDatabase=FirebaseDatabase.getInstance();
databaseReference=firebaseDatabase.getReference("Posts");
Query query = databaseReference.orderByKey();
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
{
Listdata listdata=dataSnapshot1.getValue(Listdata.class);
list.add(listdata);
}
notesAdapter.notifyDataSetChanged();
}
Upvotes: 2
Views: 122
Reputation: 97
you have to clear the list before you get info from database, then add elements and do adapter.notifySetDataChange().
Upvotes: 2