I want to delete the TextView string value from the firebase database which after deleting will return the value as true. How can we then integrate more qr code values under the same key qrCode as shown in the image below.
Image for database :
Basically a qr code scanner will be inbuilt in this app which will give us the string value in TextView field which we want to verify with our database on firebase.
package com.vidmun.dhananjay.qrscanner;
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
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button delete = (Button) findViewById(R.id.delete);
TextView qrCode = (TextView) findViewById(R.id.qrCode);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mainTest();
//Toast.makeText(getApplicationContext() , "Pressed", Toast.LENGTH_LONG).show();
}
});
}
public void mainTest() {
final TextView qrCode = (TextView) findViewById(R.id.qrCode);
final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference("qrCode");
rootRef.child("qrCode").orderByChild("data").equalTo(String.valueOf(qrCode)).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild("qrCode/data")) {
dataSnapshot.getRef().removeValue();
Toast.makeText(getApplicationContext() , "Done", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext() , "Not Done", Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
}
The expected output is the execution of the if condition but else condition is being executed.
Try this
private static final String TAG = MainActivity.class.getSimpleName();
// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("qrCode");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myRef.addChildEventListener(new MyChildEventListener());
}
public void deleteValueFromDb(View view){
myRef.child("data").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
dataSnapshot.getRef().removeValue();
}else{
Toast.makeText(getApplicationContext() , "Not Done", Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
private class MyChildEventListener implements ChildEventListener {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.i(TAG, "childAdded " + dataSnapshot.toString());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Log.i(TAG, "childChanged " + dataSnapshot.toString());
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log.i(TAG, "childRemoved " + dataSnapshot.toString());
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
Log.i(TAG, "childMoved " + dataSnapshot.toString());
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i(TAG, "childMoved " + databaseError.getMessage());
}
}
I believe the issue is with the condition you are checking in onDatachange(). Because of the path "qrCode/data" will always return false.
You can change the "data" with your intended value from editText filed.
And also you can use custom callback listener to get notified of the value when removed.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments