gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:41:26 25 4
gpt4 key购买 nike

我正在尝试开发一个Android应用程序,我可以在其中删除该值(如果它存在)。如果值被删除,它应该给出 true,如果值不存在,它应该抛出一个 false 错误。我正在使用 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();

}

}



出现的错误

error: is not abstract and does not override abstract method onDataChange(DataSnapshot) in ValueEventListener

最佳答案

当您实现ValueEventListener时,您必须实现该接口(interface)的两个方法: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 中的方法。

关于java - 无法从实时数据库中删除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58257568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com