Firebase RealTime Database Delete First 20 item

53
October 16, 2018, at 2:10 PM
DatabaseReference newReferance = database.getReference().child("Users");
    Query query = newReferance.orderByChild("timestamp");
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            chatMessages.clear();
            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                HashMap<String, String> hashMap = (HashMap<String, String>) dataSnapshot1.getValue();
                //String userMail = hashMap.get("useremail");
                String userMessage = hashMap.get("usermessage");
                String userCt = hashMap.get("usershow");
                chatMessages.add(userCt + ": " + userMessage);
                if (chatMessages.size() >= 10){
                    for (int i= 0; i < 5; i++){
                        //HOW ????
                    }
                }
                recyclerAdapter.notifyDataSetChanged();
            }
        }

Hi Dear guys. How do I select and delete the first 5 data in a real-time database? When the ArrayList reaches a certain limit, I want to delete the first 5 data from the database. I'd appreciate it if you could help with the problem.

Answer 1
final DatabaseReference newReferance = database.getReference().child("Users");
    final Query query = newReferance.orderByChild("timestamp");
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            chatMessages.clear();
            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                HashMap<String, String> hashMap = (HashMap<String, String>) dataSnapshot1.getValue();
                //String userMail = hashMap.get("useremail");
                String userMessage = hashMap.get("usermessage");
                String userCt = hashMap.get("usershow");
                chatMessages.add(userCt + ": " + userMessage);
                if (chatMessages.size() >= 10){
                    Query query1 = newReferance.limitToFirst(5);
                    query.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            database.getReference().child("Users").setValue(null);
                        }
                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {
                        }
                    });
                }
                recyclerAdapter.notifyDataSetChanged();
            }
        }

Although I'm set, it's all deleted. I can't erase the items I want. All cleared.

Answer 2

rootRef is the firebase database reference. whose instance we have to gained before trying to delete any value. you can see the code given below.

count = 0;
    rootRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                CommonLocationClass user = postSnapshot.getValue(CommonLocationClass.class);
                if (count < 5) {
                    rootRef.child(user.getPhNo).removeValue();
                    count++;
                }
            }
        }
    }
    @Override
    public void onCancelled (DatabaseError firebaseError){
    }
});

the CommonLocationClass is a getter setter model class i have made to make the fire-base implementation easy

you can try this it works for me

READ ALSO
Search SQLite DB for DateType via String - Android

Search SQLite DB for DateType via String - Android

I currently use a SQLite database in an existing Android project

64
Custom post type - related products

Custom post type - related products

I've made a custom post type "product" on my Wordpress siteThe detail page of a product is single-product

53
jQuery DataTables: Avoid linebreaks in cells

jQuery DataTables: Avoid linebreaks in cells

I don't have a CSS problem, but a logical problemI want to get rid of automatic linebreaks in my table cells

51