Yousra
Yousra

Reputation: 1

Save ArrayList in Firebase Database

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:

enter image description here

Upvotes: 0

Views: 7969

Answers (1)

elbert rivas
elbert rivas

Reputation: 1464

You can't store arrays in firebase. It's either you restructure your database or you store members in string form like MemberA, MemberB, MemberC or their id's respectively. So you can do set the members value like below.

ArrayList<String> namesList = new ArrayList<>(set1);
String strNameList = "";
for (int i = -; i < namesList.size(); i++) {
    strNameList += namesList[i] + ",";
}

ref2.setValue(strNameList);

Or you can restructure your db. I personally recommend this.

groups
   groupA
      admin

members
   groupA
      0: "Alpha"
      1: "Bravo"

So if you want to know the members of groupA then you can do this.

// considering that you have the group name value
// Ex. groupA
database.child("members").child("groupA"). addValueEventListener(...)

You can check documentation here on how to structure your data

Upvotes: 2

Related Questions