How to delete value from the database which is entered into my textView after pressing delete button?

Dhananjay

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.

Nikhil Seban

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.

edited at
0

Comments

0 comments
Login to comment

Related

My netbeans thinks that the value which entered into the textfield is a column name and not the value which i need it to delete

Delete a row by pressing the button

How to delete from my database in PHP/mysql?

how to delete a db line by pressing button delete with an ajax call

Appcrash: How to delete a character in edittext by pressing delete button?

if my database is empty then how will be auto hide my delete button

how can I delete a database from my phpmyadmin dashboard which named with multiple strings like "coming soon"?

How to delete rows after the item which equals to exact value?

How do I delete value from one textfield after the other as long as x button on keyboard is pressed

How do I delete a row from my database based on a post value within the row

How to change TextView in Activity by delete button in RecyclerView?

Delete id if not in database from a value

Sql delete value from database

How to Delete Firebase DataBase from onClick listener of Button

How to delete listview item from database using button?

How to delete an object by pressing a button with onClick via mutation

How to delete the specific database value

How to delete all records from the database that match my criteria

on Update, if one of my param is blank, how to delete it from database?

delete button delete record from the database but not from the frontend

How can I delete integers from list (of integers and strings) by taking which value to delete from user input?

How can I get the value from a cell which is in the same row as the button I am pressing

How can I delete data from database after 1 hour?

How to automatically delete records from database after a specific time in Django

How to delete from database after tests insert data using PHPUnit?

How can delete row from database using each delete button on datagrid

How detect which delete button was clicked?

How to get row value from table when delete button is clicked?

Delete row from database with a button in table (PHP)