I use the realtime database to store some values.
I want to update these values via a button-click.
I tried using setValue and updateChildren but both did not work. I get no errors and the app does not crash. The updating works when I restart the app but only once. After the first time, the values get added as new values.
public void PostToDatabase(String databasestring, ArrayList<Integer> arrayList)
{
database = FirebaseDatabase.getInstance();
myRef = database.getReference(databasestring);
Map<String,Object> postvalues = new HashMap<>();
for (int i = 0;i<arrayList.size();i++) {
postvalues.put(myRef.child(String.valueOf(i)).getKey(),arrayList.get(i));
}
myRef.updateChildren(postvalues);
}
seems like "i" does not reset after each click.
*****EDIT*****
public void PostToDatabase(String databasestring, ArrayList<Integer> arrayList)
{
database = FirebaseDatabase.getInstance();
myRef = database.getReference(databasestring);
Map<String, Object> childUpdates = new HashMap<>();
for (int i = 0;i<arrayList.size();i++) {
MyPacket myPacket = new MyPacket(arrayList.get(i).toString(), false, true, false);
Map<String, Object> postValues = myPacket.toMap();
childUpdates.put(myRef.child("Numbers").push().getKey(),postValues);
}
myRef.updateChildren(childUpdates).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful())
{
Log.d(TAG,"onComplete");
}
else
{
Log.e(TAG,"onComplete", task.getException());
}
}
});
}
The call of the Funktion
B_weiter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int index1 = 0; index1 < (tableLayout).getChildCount(); ++index1) {
TableRow upperChild = (TableRow) (tableLayout).getChildAt(index1);
for (int index2 = 0; index2 < ((ViewGroup) upperChild).getChildCount(); ++index2) {
EditText lowerChild = (EditText) ((ViewGroup) upperChild).getChildAt(index2);
myNumbers.add(Integer.parseInt(lowerChild.getText().toString()));
}
}
((WaeschelisteActivity)getActivity()).PostToDatabase("Numbers",myNumbers);
myNumbers.clear();
}
});
I created a class for my values but it still does not work. Same Problem. I hope you guys can help me.
public void PostToDatabase(String databasestring, ArrayList<Integer> arrayList)
{
database = FirebaseDatabase.getInstance();
myRef = database.getReference(databasestring);
Map<String, Object> childUpdates = new HashMap<>();
for (int i = 0;i<arrayList.size();i++) {
MyPacket myPacket = new MyPacket(arrayList.get(i).toString(), false, true, false);
Map<String, Object> postValues = myPacket.toMap();
childUpdates.put("/user-posts/" + i ,postValues);
}
myRef.updateChildren(childUpdates);
childUpdates.clear();
}
i got rid of the key and added "/user-posts/" to childUpdates it works !!! Thanks for your help!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.