I created an array in the database. Through the application code, I want to get the value from the array and delete it and the database, so that the code needs to work so that the value is completely removed, that is, the array must begin again with “0”, but with the help of the removeValue () command it simply assigns zero. How can I delete a value from the database?

Program Code:

import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; 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; import java.util.Map; public class MainActivity extends AppCompatActivity { Button mButtonSee; TextView mTextViewPromo; DatabaseReference mRef = FirebaseDatabase.getInstance().getReference(); DatabaseReference mPromoRef = mRef.child("delivery").child("0").child("promokod"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButtonSee = findViewById(R.id.buttonSee); mTextViewPromo = findViewById(R.id.promo); mButtonSee.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mPromoRef.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { String text = dataSnapshot.getValue(String.class); mTextViewPromo.setText(text); mPromoRef.removeValue(); } @Override public void onCancelled(@NonNull DatabaseError databaseError) { } }); } }); } } 

As a result, I want to get the value from the first array when I click a button, then delete it to get the value from the new first array again.

My DB:

enter image description here

I turned to the English-language forum, where one person suggested changing the code to:

 public class MainActivity extends AppCompatActivity { Button mButtonSee; TextView mTextViewPromo; DatabaseReference mRef = FirebaseDatabase.getInstance().getReference(); DatabaseReference mPromoRef = mRef.child("delivery"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButtonSee = findViewById(R.id.buttonSee); mTextViewPromo = findViewById(R.id.promo); mButtonSee.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mPromoRef.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { LinkedList<String> values = new LinkedList<>(); boolean isFirst = true; for (DataSnapshot codeSnapshot: dataSnapshot.getChildren()) { if (isFirst) { isFirst = false; } else { String text = codeSnapshot.getValue(String.class); values.add(text); } } mPromoRef.setValue(values); } @Override public void onCancelled(@NonNull DatabaseError databaseError) { throw databaseError.toException(); } }); } }); } } 

but now an error pops up when a button is pressed:

 > 2019-02-22 21:33:43.502 4777-4777/com.example.ru.puddig > E/AndroidRuntime: FATAL EXCEPTION: main > Process: com.example.ru.puddig, PID: 4777 > com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String > at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertString(com.google.firebase:firebase-database@@16.0.6:413) > at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.0.6:199) > at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@16.0.6:79) > at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@16.0.6:212) > at com.example.ru.puddig.MainActivity$1$1.onDataChange(MainActivity.java:54) > at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@16.0.6:183) > at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.6:75) > at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.6:63) > at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.6:55) > at android.os.Handler.handleCallback(Handler.java:873) > at android.os.Handler.dispatchMessage(Handler.java:99) > at android.os.Looper.loop(Looper.java:193) > at android.app.ActivityThread.main(ActivityThread.java:6669) > at java.lang.reflect.Method.invoke(Native Method) > at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) > at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) > 2019-02-22 21:33:43.574 4777-4777/com.example.ru.puddig I/Process: > Sending signal. PID: 4777 SIG: 9 

Apparently the guy from the forum could not stand my stupidity and is silent for several days)

Help me figure out what's wrong. link to english version: https://stackoverflow.com/questions/54745930/how-to-completely-remove-the-value-from-the-firebase-realtime-database

  • Alternatively, you can simply assign an array with 0 - Just Master of Chaos
  • Can it be more specific? - Ara Gevorgyan

0