上周我一直在努力解决这个问题,但我找不到任何东西似乎对我有所帮助。我正在使用Firebase与用户一起创建Android应用,并且每个用户都有与其他用户交互的历史记录,因此我希望在主页上的列表中为任何给定用户显示此历史记录。这就是我的数据库在JSON格式中的样子:
"app" {
"history" {
"AAA000" {
"0" {
"uid": "BBB111"
}
"1" {
"uid": "CCC222"
}
}
"BBB111" {
"0" {
"uid": "AAA000"
}
}
"CCC222" {
"0" {
"uid": "AAA000"
}
}
}
"users" {
"AAA000" {
"first_name": "John"
"last_name": "Doe"
"username": "johndoe"
}
"BBB111" {
"first_name": "Jane"
"last_name": "Doe"
"username": "janedoe"
}
"CCC222" {
"first_name": "Alice"
"last_name": "Doe"
"username": "alicedoe"
}
}
}
因此,如果AAA000是登录用户,我希望显示一个列表,其中一个元素包含来自BBB111的信息,第二个元素包含来自CCC222的信息。但是,我试图这样做的方式,列表中没有任何内容。这是我的主页活动中RecyclerView
的代码:
if (FirebaseAuth.getInstance().getCurrentUser() != null) {
class Dummy {
private String uid;
public Dummy(String uid) {
this.uid = uid;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
}
final Dummy dummy = new Dummy(FirebaseAuth.getInstance().getCurrentUser().getUid());
RecyclerView recycler = (RecyclerView) findViewById(R.id.list);
recycler.setHasFixedSize(true);
recycler.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerAdapter<Dummy, HistoryHolder> adapter = new FirebaseRecyclerAdapter<Dummy, HistoryHolder>(Dummy.class, R.layout.list_item,
HistoryHolder.class, FirebaseDatabase.getInstance().getReference("history").child(dummy.getUid())) {
public void populateViewHolder(final HistoryHolder user_holder, Dummy user, int position) {
DatabaseReference user_reference = FirebaseDatabase.getInstance().getReference("users").child(user.getUid());
user_reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
user_holder.name.setText(dataSnapshot.child("first_name").getValue().toString());
user_holder.username.setText(dataSnapshot.child("username").getValue().toString());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
recycler.setAdapter(adapter);
}
这里是HistoryHolder
:
private class HistoryHolder extends RecyclerView.ViewHolder {
public TextView name;
public TextView username;
public HistoryHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.item_name);
username = (TextView) view.findViewById(R.id.item_username);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do stuff
}
});
}
}
每当我运行应用程序时,RecyclerView
似乎都是空的,我不知道为什么。我对此非常陌生,所以任何建议(关于如何解决我的问题以及如何一般地改进我的代码)都将非常感激!!!