I am building a mobile messaging application,
I want to create a chat group where the user selects the members using a checkbox then save the identifiers of the selected users in a list which will be saved in the Firebase database,
The problem that my application crash when I want to save the users to the database
My logcat:
com.google.firebase.database.DatabaseException: Expected a List while deserializing, but got a class java.lang.String
Code:
private void CreateNewGroup(final String groupName) {
groupNameRef = FirebaseDatabase.getInstance().getReference().child("Groups");
DatabaseReference push = groupNameRef.push();
String push_id = push.getKey();
assert push_id != null;
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Groups").child(push_id);
Map<String, String> hasMap = new HashMap<>();
hasMap.put("groupName", groupName);
hasMap.put("Admin", firebaseUser.getUid());
hasMap.put("members", "");
ref.setValue(hasMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(SelectGroupActivity.this, "Saved" + groupName, Toast.LENGTH_SHORT).show();
}
});
DatabaseReference ref2 = FirebaseDatabase.getInstance().getReference("Groups" + "/" +
push_id + "/" + "members");
SharedPreferences myPref2 = SelectGroupActivity.this.getSharedPreferences("data2", Context.MODE_PRIVATE);
Set<String> set1 = myPref2.getStringSet("key2", null);
assert set1 != null;
ArrayList<String> namesList = new ArrayList<>(set1);
ref2.setValue(namesList).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task)
{
if(task.isSuccessful())
{
Toast.makeText(SelectGroupActivity.this, "Success", Toast.LENGTH_SHORT).show();
}
}
});
}
And my Model class
import java.util.List;
public class Groups
{
private String groupName;
private List<String> members;
private String Admin;
public Groups() {}
public Groups(String groupName, List<String> members, String Admin) {
this.groupName = groupName;
this.members = members;
this.Admin = Admin;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public List<String> getMembers() {
return members;
}
public void setMembers(List<String> members) {
this.members = members;
}
public String getAdmin() {
return Admin;
}
public void setAdmin(String admin) {
Admin = admin;
}
}
Database:
