我有一组用户名(ids),我需要使用Firebase数据库进行搜索并使用Firebase UI进行渲染,我这样做的方式只显示最后一个用户而不是所有拥有这些ID的用户。我需要使用不同的ID呈现卡片或所有用户。
这是我的班级
public class UserMessageListFragment extends Fragment {
private ProgressDialog progressDialog;
private final String TAG = "UsersMessageList";
private RecyclerView mResultList;
private ArrayList<String> arrayUsers;
public static UserMessageListFragment newInstance() {
return new UserMessageListFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_user_message_list, container, false);
mResultList = view.findViewById(R.id.message_list);
mResultList.setHasFixedSize(true);
mResultList.setLayoutManager(new LinearLayoutManager(mResultList.getContext()));
final DatabaseReference refMessages = FirebaseDatabase.getInstance().getReference();
final DatabaseReference ref= FirebaseDatabase.getInstance().getReference().child("usuarios");
refMessages.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if(dataSnapshot.getKey().contains(UserDetails.username+ "_")) {
String[] userChatWithId = dataSnapshot.getKey().split(UserDetails.username+"_");
final Query firebaseSearchQuery = ref.orderByChild("username").equalTo(userChatWithId[1]);
final FirebaseRecyclerOptions<User> options =
new FirebaseRecyclerOptions.Builder<User>()
.setQuery(firebaseSearchQuery, User.class)
.build();
bindAndBuildFirebaseUi(options);
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
// progressDialog = new ProgressDialog(getContext());
// progressDialog.setMessage("Carregando...");
// progressDialog.show();
return view;
}
public void bindAndBuildFirebaseUi(FirebaseRecyclerOptions options) {
final FirebaseRecyclerAdapter<User, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<User, UsersViewHolder>(options) {
@Override
public UsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_list_layout, parent, false);
return new UsersViewHolder(v);
}
@Override
protected void onBindViewHolder(final UsersViewHolder holder, int position, final User model) {
holder.bind(model);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UserDetails.chatWithId = model.getUsername();
UserDetails.chatWith = model.getName();
startActivity(new Intent(getContext(), Chat.class));
}
});
}
};
firebaseRecyclerAdapter.startListening();
mResultList.setAdapter(firebaseRecyclerAdapter);
}
}
在这一行
final Query firebaseSearchQuery = ref.orderByChild("username").equalTo(userChatWithId[1]);
userChatWithid 1中会有不同的值,但FirebaseUI只渲染最后一个。
我开始讨厌FirebaseUI,因为我一直有这么多问题。
有人可以帮助我吗?
答案 0 :(得分:1)
您正在做一些非常规的事情...... FirebaseUI将为您处理所有数据库侦听器和查询。您应该做的只是传递包含所有用户的根节点,其余的将被处理。 (我相信你的情况是usuarios
所以ref
可以解决问题。)我建议你阅读文档:https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md