无法从数据库中检索Post键和值。这是我第一次尝试使用FirebaseRecyclerAdapter从Firebase数据库中检索数据。但我不知道问题出在哪里。我也尝试调试,但编译器不会进入onCreateViewHolder或onBindViewHolder函数。但我完全按照FirebaseUI进行实时数据库参考。
NEWSFEEDBANK.JAVA
private FirebaseRecyclerAdapter<NewsFeed, NewsFeedViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_feed_bank);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
SharedPreferences sharedPreferences = getSharedPreferences("BankData", Context.MODE_PRIVATE);
username = sharedPreferences.getString("UserName", "UserName");
recyclerView = (RecyclerView) findViewById(R.id.recycler_View_Bank);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mDatabase = FirebaseDatabase.getInstance().getReference().child("Posts");
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (mAuth.getCurrentUser() == null) {
Intent loginIntent = new Intent(NewsFeedBank.this, Login.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(loginIntent);
}
}
};
DisplayPosts();
}
private void DisplayPosts() {
FirebaseRecyclerOptions<NewsFeed> options = new FirebaseRecyclerOptions.Builder<NewsFeed>().setQuery(mDatabase, NewsFeed.class).build();
adapter = new FirebaseRecyclerAdapter<NewsFeed, NewsFeedViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull NewsFeedViewHolder holder, int position, @NonNull NewsFeed model) {
// holder.title.setText(model.getTitle());
// holder.des.setText(model.getDesc());
holder.usern.setText(model.getUsername());
}
@NonNull
@Override
public NewsFeedViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_items, viewGroup, false);
NewsFeedViewHolder holder = new NewsFeedViewHolder(view);
return holder;
}
};
recyclerView.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
public static class NewsFeedViewHolder extends RecyclerView.ViewHolder {
TextView title, des, usern;
View mView;
public NewsFeedViewHolder(@NonNull View itemView) {
super(itemView);
usern = (TextView) mView.findViewById(R.id.view_poster_username);
}
}}
NEWSFEED.JAVA
导入com.google.firebase.storage。存储参考;
公共类NewsFeed{
private String Title, Description ,UserName;
private StorageReference imageUrl;
public NewsFeed(String UserName, String Title, String Description) {
this.Title = Title;
this.Description = Description;
this.UserName = UserName;
}
public NewsFeed() {
}
public void setUsername(String UserName) {
this.UserName = UserName;
}
public void setTitle(String Title) {
this.Title = Title;
}
public void setDesc(String Description) {
this.Description = Description;
}
public String getTitle() {
return Title;
}
public String getDesc() {
return Description;
}
public String getUsername() {
return UserName;
}}
试试这个
usern = (TextView) itemView.findViewById(R.id.view_poster_username);
代替
usern = (TextView) mView.findViewById(R.id.view_poster_username);
如果您再次收到错误,请发布您的布局文件