How to delete data from Firebase in Android Studio

Android Studio is one of the software The best Android application developer today. This is due to the many features that are supported by Google. In the previous tutorial, we discussed how to connect Android Studio to Firebase and add data to Firebase. This time we will discuss how to delete data from Firebase in Android studio.

Also read:

It was done in the previous tutorial shape with entrance Username, Name and number WL. This time we’re going to add more, namely Username and button to remove Username. When the button is pressed, the data Username on Firebase will be deleted. Here is the tutorial.

Tutorials

Activity_main.xml

1. Write down the following code xml-activity_main.xml

                         

When button Delete is pressed, then method click2 is executed.

2. Then continue Main activity.java adds a couple of changes, namely instantiation text4 what is entry Username new and method click2, which is used to delete data on Firebase.

MainActivity.java

Write down the following code MainActivity.java

package com.example.abifirmandhani.inwepo;  import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView;  import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase;  public class MainActivity extends AppCompatActivity {      	//instansiasi database firebase     	FirebaseDatabase database ;     DatabaseReference myRef;     EditText text1;     EditText text2;     EditText text3;     EditText text4;        @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         database = FirebaseDatabase.getInstance();         text1 = (EditText) findViewById(R.id.edit1);          text2 = (EditText) findViewById(R.id.edit2);         text3 = (EditText) findViewById(R.id.edit3);         text4 = (EditText) findViewById(R.id.edit4);        }      	//method tambah user     	public void Click1(View view){          //Referensi database yang dituju         myRef = database.getReference("User").child(text1.getText().toString());          //memberi nilai pada referensi yang dituju         myRef.child("name").setValue(text2.getText().toString());         myRef.child("NoHp").setValue(text3.getText().toString());       }      //Hapus user     public void Click2(View view){          //inisiasi database reference         myRef = database.getReference("User").child(text4.getText().toString());          //hapus data         myRef.removeValue();       }  }

Databases R.the reference is child from user, where child of the user according to the wishes of the user.

Then delete the required data with method removeValue ().

3. Run the application by clicking the play icon in the Toolbar Android Studio and select device that you will use for testing.

4. Here is a picture where Database before something is deleted.

5. Then the application tries to delete data Username aaa.

6. In the picture seen that data Username aaa was successfully deleted, leaving behind cupacup and wikipedia.

7. Will try again to delete Username Cup.

8th Dan Username cupacup was also successfully deleted and left Username Wikipedia.

Complete. The above tutorial is a simple example of clearing data on Firebase. You can expand it again by changing some code in the reference database.






Leave a Reply

Your email address will not be published.