从Firebase获取特定用户的正确数据

时间:2018-08-29 15:37:17

标签: android firebase firebase-realtime-database

我有一个聊天应用程序,我正在尝试下拉已登录用户所属的聊天组。我正在努力争取合适的团体。 Here is my firebase structure。所有成员都在组内的“成员”子级下。我正在正常工作的recyclerview中显示组。对于任何登录的用户来说,这当然都是可行的,并且该用户可以属于许多不同的组。希望您能想到,并能对我有所帮助。谢谢

我的代码:

private FirebaseDatabase mDatabase;
private DatabaseReference mDatabaseReference;
private FirebaseAuth mAuth;
private FirebaseUser mUser;
private String projectId;
private List<DataService> grouplist;
private RecyclerAdapter recyclerAdapter;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mAuth = FirebaseAuth.getInstance();
    mUser = mAuth.getCurrentUser();
    mDatabaseReference = mDatabase.getReference().child("groups");

    mDatabase = FirebaseDatabase.getInstance();
    mDatabaseReference.keepSynced(true);

    grouplist = new ArrayList<>();
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
}

protected void onStart() {
    super.onStart();
    grouplist.clear();

        mDatabaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            DataService groupData = dataSnapshot.getValue(DataService.class);

            grouplist.add(groupData);

            recyclerAdapter = new RecyclerAdapter(Main.this, grouplist);
            recyclerView.setAdapter(recyclerAdapter);
            recyclerAdapter.notifyDataSetChanged();

        }


        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

DataService.java

public class DataService {
public String subject;
public String projectName;
public String endDate;
public String members;
public String userid;
public String creationDate;
public String dropbox;
public String evernote;
public String googleDocs;

public DataService() {
}


public DataService(String subject, String projectName, String endDate, String members, String userid, String creationDate, String dropbox, String evernote, String googleDocs) {
    this.subject = subject;
    this.projectName = projectName;
    this.endDate = endDate;
    this.members = members;
    this.userid = userid;
    this.creationDate = creationDate;
    this.dropbox = dropbox;
    this.evernote = evernote;
    this.googleDocs = googleDocs;
}

public String getCreationDate() {
    return creationDate;
}

public void setCreationDate(String creationDate) {
    this.creationDate = creationDate;
}

public String getDropbox() {
    return dropbox;
}

public void setDropbox(String dropbox) {
    this.dropbox = dropbox;
}

public String getEvernote() {
    return evernote;
}

public void setEvernote(String evernote) {
    this.evernote = evernote;
}

public String getGoogleDocs() {
    return googleDocs;
}

public void setGoogleDocs(String googleDocs) {
    this.googleDocs = googleDocs;
}

public String getSubject() {
    return subject;
}

public void setSubject(String subject) {
    this.subject = subject;
}

public String getProjectName() {
    return projectName;
}

public void setProjectName(String projectName) {
    this.projectName = projectName;
}

public String getEndDate() {
    return endDate;
}

public void setEndDate(String endDate) {
    this.endDate = endDate;
}

public String getmembers() {
    return members;
}

public void setmembers(String members) {
    this.members = members;
}

public String getUserid() {
    return userid;
}

public void setUserid(String userid) {
    this.userid = userid;
}
}

1 个答案:

答案 0 :(得分:0)

使用实际的数据库结构,除非您对数据库进行了某些更改,否则您无法获得用户所在的所有组。在您的情况下,您应该考虑通过在每个用户对象下创建另一个名为 Container( margin: EdgeInsets.only(bottom: 50), child: Container( decoration: BoxDecoration( color: snapshot.data.color, image: DecorationImage( image: AssetImage("assets/overlay.png"), fit: BoxFit.cover, ), ), ), ), 的节点来扩展数据库结构,以允许反向查找,在该节点中,您应添加用户所属的所有组ID。因此您的数据库结构应如下所示:

groups

要获得用户的所有网上论坛,只需在Firebase-root | --- users | --- userId | --- groups | --- groupIdOne: true | --- groupIdTwo: true 节点上附加一个侦听器,便有您需要的所有网上论坛。

但是您会思考,为什么要这样做?为什么要重复数据?好吧,关于Firebase,复制数据没有问题。这是一种非常普遍的做法,命名为groups,为此,我建议您观看此视频Denormalization is normal with the Firebase Database

复制数据时,请记住一件事。用与添加数据相同的方式,您需要对其进行维护。换句话说,如果您想更新/删除项目,则需要在它存在的每个位置进行。