我习惯将populateViewHolder
与firebase回收器适配器一起使用,但onBindViewHolder
是根据this post执行此操作的新方法。我试过onBindViewHolder
,但它没有显示任何内容。
我做错了什么?
这是我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
mUsersList = (RecyclerView) findViewById(R.id.usersList);
mUsersList.setHasFixedSize(true);
mUsersList.setLayoutManager(new LinearLayoutManager(this));}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<Users> options =
new FirebaseRecyclerOptions.Builder<Users>()
.setQuery(mDatabase, Users.class)
.build();
FirebaseRecyclerAdapter<Users, UsersViewHolder> adapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(options) {
@Override
public UsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.users_single_layout, parent, false);
return new UsersViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull Users model) {
holder.setName(model.getName());
}
};
mUsersList.setAdapter(adapter);
}
public static class UsersViewHolder extends RecyclerView.ViewHolder {
View mView;
public UsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name){
TextView userNames = (TextView) mView.findViewById(R.id.textView2);
userNames.setText(name);
}
}}
我的模特班的代码。
public class Users {
public String name;
public String image;
public String status;
public Users(){
}
public Users(String name, String image, String status) {
this.name = name;
this.image = image;
this.status = status;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}}
我的自定义布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/common_google_signin_btn_icon_dark_focused"
android:id="@+id/circleImageView2" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="89dp"
android:layout_marginStart="89dp"
android:layout_toEndOf="@+id/circleImageView2"
android:layout_toRightOf="@+id/circleImageView2"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/circleImageView2"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:text="TextView" />
</RelativeLayout>
答案 0 :(得分:0)
我可以在firebase中看到您的表用户吗?添加 onCreateViewHolder 和 onBindViewHolder 日志以查看是否已调用。
答案 1 :(得分:0)
确保在firebase项目中设置读取规则。将read设置为true。
答案 2 :(得分:-1)
您可以添加:
adapter.startListening();
之后:
mUsersList.setAdapter(adapter);
它应该看起来像:
mUsersList.setAdapter(adapter);
adapter.startListening();