无法从Firebase实时数据库删除数据

时间:2020-06-26 07:39:46

标签: java android firebase firebase-realtime-database

我检查了很多答案,还实现了用于从Firebase数据库删除数据的代码。但是,当我单击“删除”按钮时,没有任何反应,数据不会被删除。我不明白为什么会出现这个问题。

这是我的代码:

    deletebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String subcategory = spinnersub.getSelectedItem().toString();
                spinnersub.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        if (parentcategory.equals("Jokes")) {
                            subCategoryQuery = FirebaseDatabase.getInstance().getReference("Jokes").orderByChild("subcategory")
                                    .equalTo(subcategory);

                            subCategoryQuery.addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    for (DataSnapshot subSnapshot : dataSnapshot.getChildren()) {
                                        subSnapshot.getRef().removeValue();
                                    }
                                }

                                @Override
                                public void onCancelled(DatabaseError databaseError) {
                                    Log.e("TAG", "onCancelled", databaseError.toException());
                                }
                            });
                        }
                        else if(parentcategory.equals("Shayari")){
                            subCategoryQuery = FirebaseDatabase.getInstance().getReference("Shayari").orderByChild("subcategory")
                                    .equalTo(subcategory);

                            subCategoryQuery.addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    for (DataSnapshot subSnapshot : dataSnapshot.getChildren()) {
                                        subSnapshot.getRef().removeValue();
                                    }
                                }

                                @Override
                                public void onCancelled(DatabaseError databaseError) {
                                    Log.e("TAG", "onCancelled", databaseError.toException());
                                }
                            });

                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
            }
        });

请告诉我我做错了什么?为什么它不起作用?为什么数据没有删除?

0 个答案:

没有答案