无法从实时数据库中删除值

时间:2019-10-06 13:11:12

标签: java android firebase firebase-realtime-database

我正在尝试开发一个Android应用程序,如果其中存在该值,我可以删除其中的值。如果删除了值,则该值应为true;如果该值不存在,则应抛出错误。我正在使用Firebase实时数据库。

我正在尝试执行if-else语句,但是我无法成功

package com.vi.dhananjay.lol;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.view.View;
import android.widget.Button;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button del = (Button) findViewById(R.id.delButton);
        Button eb = (Button) findViewById(R.id.ebDelete);
        Button teac = (Button) findViewById(R.id.teaDelete);
        Button csd =  (Button) findViewById(R.id.conDelete);
        Button hosp = (Button) findViewById(R.id.hosDelete);

        TextView qrCode = (TextView) findViewById(R.id.qrCode);


        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
        rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            void deleDelete(DataSnapshot snapshot) {
                if (snapshot.hasChild("qrCode")) {
                    // run some code
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });


        eb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    ebDelete();
            }
        });
        teac.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    teacDelete();
            }
        });
        csd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    csdDelete();
            }
        });



        hosp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hospDelete();
            }
        });

        del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleDelete();
            }
        });
    }

    private void deleDelete() {
        DatabaseReference qrcode = FirebaseDatabase.getInstance().getReference("delegates");
        qrcode.removeValue();
        Toast.makeText(this, "Code validated", Toast.LENGTH_LONG).show();
    }
    private void ebDelete() {
        DatabaseReference qrcode = FirebaseDatabase.getInstance().getReference("delegates");
        qrcode.removeValue();
        Toast.makeText(this, "Code validated", Toast.LENGTH_LONG).show();
    }
    private void teacDelete() {
        DatabaseReference qrcode = FirebaseDatabase.getInstance().getReference("delegates");
        qrcode.removeValue();
        Toast.makeText(this, "Code validated", Toast.LENGTH_LONG).show();
    }
    private void csdDelete() {
        DatabaseReference qrcode = FirebaseDatabase.getInstance().getReference("delegates");
        qrcode.removeValue();
        Toast.makeText(this, "Code validated", Toast.LENGTH_LONG).show();
    }
    private void hospDelete() {
        DatabaseReference qrcode = FirebaseDatabase.getInstance().getReference("delegates");
        qrcode.removeValue();
        Toast.makeText(this, "Code validated", Toast.LENGTH_LONG).show();

    }

}



出现的错误

  

错误:不是抽象的,并且不会覆盖ValueEventListener中的抽象方法onDataChange(DataSnapshot)

1 个答案:

答案 0 :(得分:0)

实现ValueEventListener时,必须实现该接口的两种方法:onDataChangeonCancelled。看来您将onDataChange的名称更改为deleDelete,这意味着编译器不再能找到onDataChange,从而导致错误。

要解决此问题,请将deleDelete重命名为onDataChange

rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    void onDataChange(DataSnapshot snapshot) {
        if (snapshot.hasChild("qrCode")) {
            // run some code
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }
});

如果您想使用deleDelete方法名称来简化说明,可以将其包含在实现中。您只需要确保仍然实现onDataChange,否则编译器将不知道该怎么做。

类似这样:

rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    void onDataChange(DataSnapshot snapshot) {
        deleteDelete(snapshot)
    }

    void deleteDelete(DataSnapshot snapshot) {
        if (snapshot.hasChild("qrCode")) {
            // run some code
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }
});

您会注意到,deleteDelete在这里没有用@Override标记,因为它没有覆盖/实现ValueEventListener中的方法。