篇首语:本文由编程笔记#小编为大家整理,主要介绍了RecyclerView没有被来自firebase的项目填充,只是一个空白的活动页面。相关的知识,希望对你有一定的参考价值。
我对android和firebase还是比较陌生的,我试图从firebase数据库中填充recyclerview,但由于一些我无法理解的原因,该视图没有被填充,活动加载了,但我看到的是一个空白的活动页面,我想使用Firebase数据库中的数据来填充这些视图,只是为了获得用户的三个数据(姓名,电子邮件,电话)。
public class TestUsersActivity extends AppCompatActivity {
public TextView mname,email,phone;
private RecyclerView recyclerView;
FirebaseAuth mAuth;
DatabaseReference mDatabase;
private FirebaseRecyclerAdapter adapter;
private LinearLayoutManager linearLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_users);
recyclerView = findViewById(R.id.listuser);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
linearLayoutManager = new LinearLayoutManager(getApplicationContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(linearLayoutManager);
fetch();
}
private void fetch() {
FirebaseRecyclerOptions optiOns= new FirebaseRecyclerOptions.Builder()
.setQuery(mDatabase,Users.class)
.build();
adapter = new FirebaseRecyclerAdapter(options) {
@Override
protected void onBindViewHolder(@NonNull ViewHolder myViewHolder, int i, @NonNull Users users) {
myViewHolder.setName(users.getFname() + " "+ users.getLname());
myViewHolder.setEmail(users.getEmail());
myViewHolder.setphone(users.getPhonenos());
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_list_row,parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onDataChanged() {
super.onDataChanged();
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
mname = itemView.findViewById(R.id.name);
email = itemView.findViewById(R.id.email);
phOne= itemView.findViewById(R.id.phoneNos);
}
public void setName(String string) {
mname.setText(string);
}
public void setEmail(String string) {
email.setText(string);
}
public void setphone(String string){
phone.setText(string);
}
}
}
下面是我的modelclass,我只想获取姓名、电子邮件和电话号码。
public class Users {
private String fname;
private String lname;
private String email;
private String phonenos;
private String jobTitle;
private String dateCreated;
private String lastlogin;
public Users(){
}
public Users(String fname, String lname, String email, String phonenos, String jobTitle, String dateCreated, String lastlogin) {
this.fname = fname;
this.lname = lname;
this.email = email;
this.phOnenos= phonenos;
this.jobTitle = jobTitle;
this.dateCreated = dateCreated;
this.lastlogin = lastlogin;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhonenos() {
return phonenos;
}
public void setPhonenos(String phonenos) {
this.phOnenos= phonenos;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
public String getDateCreated() {
return dateCreated;
}
public void setDateCreated(String dateCreated) {
this.dateCreated = dateCreated;
}
public String getLastlogin() {
return lastlogin;
}
public void setLastlogin(String lastlogin) {
this.lastlogin = lastlogin;
}
}