gpt4 book ai didi

android - Firebase RecyclerView UI 未显示

转载 作者:行者123 更新时间:2023-12-01 16:39:03 24 4
gpt4 key购买 nike

我一直在尝试实现firebase ui在我的应用程序中 recyclerview 。我的 firebase 数据库中有一些我想要使用 firebase ui recycler view 的项目。到目前为止,我认为我已经做对了一切,但是,当我运行我的应用程序时,它仍然显示空白。任何帮助将不胜感激。

public class UsersActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private Query query;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users);
query = FirebaseDatabase.getInstance().getReference().child("Users");
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}

@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions <Users> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<Users>()
.setQuery(query, Users.class).build();
FirebaseRecyclerAdapter firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(firebaseRecyclerOptions){

@NonNull
@Override
public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.users_single_layout, parent,false);
return new UsersViewHolder(mView);
}

@Override
protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull Users model) {
holder.setName(model.getName());

}
};
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class UsersViewHolder extends RecyclerView.ViewHolder{
View mView;
UsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setName (String name){
TextView mName = mView.findViewById(R.id.user_name);
mName.setText(name);
}
}
}

最佳答案

您需要添加以下内容:

The FirebaseRecyclerAdapter uses an event listener to monitor changes to the Firebase query. To begin listening for data, call the startListening() method. You may want to call this in your onStart() method. Make sure you have finished any authentication necessary to read the data before calling startListening() or your query will fail.

@Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}

@Override
protected void onStop() {
super.onStop();
firebaseRecyclerAdapter.stopListening();
}

更多信息在这里:

https://github.com/firebase/FirebaseUI-Android/tree/master/database

关于android - Firebase RecyclerView UI 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61901600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com