我正在尝试开发一个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)

最佳答案

实现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中的方法。

10-08 18:01