I want to add another item to the "oreSelectate" list from my Firebase but I don't know how. I want to select another hourly interval from the app and add it to the "oreSelectate" list.
This is my app App
This is what I have in my Firebase Realtime Database. My firebase
I've tried 3 things:
- setValue() - it doesn't work because it will overwrite the existing data
- I've tried loading the firebase data in a List and then copy the firebase list to another list and add the new data to the second list and then adding that list to the child "oreSelectate" but the problem is that it keeps the old data and puts the new data like this New firebase data
- I've tried deleting the old data from firebase after loading it in a List but it still doesn't work
What seems to be the problem? I'll leave my code below:
btn_inchiriaza.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getOreFromFirebase(new OreListCallback() {
@Override
public void onCallback(List<String> value) {
ore_ocupate.clear();
ore_ocupate = value;
int len = lv_ore.getCount();
SparseBooleanArray checked = lv_ore.getCheckedItemPositions();
for(int i = 0; i < len ; i++){
if(checked.get(i)){
ore_ocupate.add((String) oreAdapter.getItem(i));
}
}
reff.child("TerenuriFotbal").child("Sector " + cifra_sector).child(nume_teren_extra).child("oreSelectate").setValue(ore_ocupate);
lv_ore.clearChoices();
oreAdapter.notifyDataSetChanged();
Toast.makeText(view.getContext(), "Ati rezervat cu succes!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(InchiriazaTeren.this, MainActivity2.class);
startActivity(intent);
}
});
}
});
public interface OreListCallback{
void onCallback(List<String> value);
}
public void getOreFromFirebase(final OreListCallback myCallback){
reff.child("TerenuriFotbal").child("Sector " + cifra_sector).child(nume_teren_extra).child("oreSelectate").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
String ora = (String) dataSnapshot.getValue();
assert ora != null;
ore_ocupate_baza_de_date.add(ora);
}
myCallback.onCallback(ore_ocupate_baza_de_date);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
Thanks!