将ArrayList保存在Firebase数据库中

时间:2019-04-19 11:09:44

标签: android firebase firebase-realtime-database

我想实现一个聊天组,其中用户使用ckeckbox选择这些朋友。我将所选用户的标识符保存在列表中,然后将该列表保存在子成员的firebase数据库中,但是我我的应用崩溃时遇到了问题。

我的logcat:

  

com.google.firebase.database.DatabaseException:反序列化时需要一个List,但是有一个类java.lang.String

代码:

 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();
            }
            }
        });
    }

还有我的Model类

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;
    }
}

数据库:

enter image description here

1 个答案:

答案 0 :(得分:0)

您无法在Firebase中存储阵列。要么是重组数据库,要么是分别以字符串形式存储成员,例如MemberA, MemberB, MemberC或它们的ID。因此,您可以像下面那样设置成员值。

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

ref2.setValue(strNameList);

或者您可以重组数据库。我个人推荐这个。

groups
   groupA
      admin

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

因此,如果您想了解groupA的成员,则可以这样做。

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

您可以查看here文档,了解如何构造数据