0
votes

I am trying to develop an android app in which I can delete the value if it presents there. If value deleted it should give true and if value doesn't present there it should throw a false error. I am using a firebase realtime database.

I am trying to make an if-else statement but I can't succeed

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();

    }

}



The error which is arising

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

1

1 Answers

0
votes

When you implement ValueEventListener, you must implement both methods of that interface: onDataChange and onCancelled. It seems you changed the name of onDataChange to deleDelete, which means that the compiler can't find onDataChange anymore, leading to the error you get.

To fix the problem, rename deleDelete back to 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
    }
});

If you'd like to use the deleDelete method name for clarity, you can include that in the implementation. You just need to ensure that you still implement onDataChange, as otherwise the compiler doesn't know what to do.

So something like:

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
    }
});

You'll note that deleteDelete is not tagged with @Override here, since it's not overriding/implementing a method from ValueEventListener.