I created an array in the database. Through the application's code I get the value from the array and I want to remove it, but for the code to work I want the value to be deleted completely, that is, the array must start again with "0", but with the help of the removeValue () command it just assigns a null. How can I remove a value from the database?
I tried both removeValue
and setValue (null)
, but it still replaces the value in the base with null.
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 to get a value from the first array when I click on the button, then delete it and again to get the value from the new first array again.
It's Database structure:
My new Code:
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();
}
});
}
});
}
}
And logcat: 2019-02-20 02:11:25.263 1631-2110/? E/AudioFlinger: not enough memory for AudioTrack size=131296
2019-02-20 02:11:25.263 1631-2110/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
2019-02-20 02:11:25.267 1631-12210/? I/AudioFlinger: AudioFlinger's thread 0xef703a00 tid=12210 ready to run
2019-02-20 02:11:25.269 2651-11949/com.google.android.googlequicksearchbox:search E/IAudioFlinger: createRecord returned error -12
2019-02-20 02:11:25.269 2651-11949/com.google.android.googlequicksearchbox:search E/AudioRecord: AudioFlinger could not create record track, status: -12
2019-02-20 02:11:25.271 2651-11949/com.google.android.googlequicksearchbox:search E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
2019-02-20 02:11:25.274 2651-11949/com.google.android.googlequicksearchbox:search E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
2019-02-20 02:11:25.274 2651-11949/com.google.android.googlequicksearchbox:search I/MicrophoneInputStream: mic_started SR : 16000 CC : 16 SO : 6
2019-02-20 02:11:25.275 2651-11949/com.google.android.googlequicksearchbox:search E/ActivityThread: Failed to find provider info for com.google.android.apps.gsa.testing.ui.audio.recorded
Run:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.ru.puddig, PID: 12504 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)