0
我正嘗試在用戶登錄後使用RecyclerView在儀表板中顯示來自Firebase數據庫的帖子。我不知道出了什麼問題。 RecyclerView沒有被填充。它甚至沒有顯示一個單一的項目。我在谷歌搜索了很多,但沒有解決方案似乎工作。爲什麼片段中的RecyclerView沒有顯示來自Firebase後端的任何數據?
Post.java:
public class Post {
private String bloodGroup;
private String postDescription;
private String postTitle;
private String timestamp;
private String userId;
public Post(){
}
public Post(String postTitle, String bloodGroup, String postDescription, String userId, String timestamp) {
this.postTitle = postTitle;
this.bloodGroup = bloodGroup;
this.postDescription = postDescription;
this.userId = userId;
this.timestamp = timestamp;
}
public String getPostTitle() {
return postTitle;
}
public void setPostTitle(String postTitle) {
this.postTitle = postTitle;
}
public String getBloodGroup() {
return bloodGroup;
}
public void setBloodGroup(String bloodGroup) {
this.bloodGroup = bloodGroup;
}
public String getPostDescription() {
return postDescription;
}
public void setPostDescription(String postDescription) {
this.postDescription = postDescription;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
DashboardFragment.java:
public class DashboardFragment extends Fragment {
private static final String TAG = "Dashboard_Fragment";
private RecyclerView postsListRcview;
View postsListView;
DatabaseReference firebaseDbUsers, firebaseDbPosts;
FirebaseRecyclerAdapter<Post, PostViewHolder> postsRecyclerAdapter;
LinearLayoutManager linearLayoutManager;
public DashboardFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
postsListView = inflater.inflate(R.layout.fragment_dashboard, container, false);
postsListView.setTag(TAG);
initVariables();
return postsListView;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
private void printMessage(String msg){
Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
}
private void initVariables(){
postsListRcview = (RecyclerView) postsListView.findViewById(R.id.posts_list_rcview);
postsListRcview.setHasFixedSize(true);
linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
postsListRcview.setLayoutManager(linearLayoutManager);
postsRecyclerAdapter = null;
postsListRcview.setAdapter(postsRecyclerAdapter);
firebaseDbUsers = FirebaseDatabase.getInstance().getReference().child("User");
firebaseDbPosts = FirebaseDatabase.getInstance().getReference().child("Post");
}
@Override
public void onStart() {
super.onStart();
postsRecyclerAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(
Post.class,
R.layout.posts_rcview_item,
PostViewHolder.class,
firebaseDbPosts
) {
@Override
protected void populateViewHolder(PostViewHolder viewHolder, Post model, int position) {
viewHolder.setTitle(model.getPostTitle());
printMessage("Hello");
}
};
postsRecyclerAdapter.notifyDataSetChanged();
postsListRcview.setAdapter(postsRecyclerAdapter);
}
public static class PostViewHolder extends RecyclerView.ViewHolder{
View mView;
TextView postTitleTv;
TextView requesterNameTv;
ImageView requesterPropic;
TextView timestampTv;
public PostViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
postTitleTv = (TextView) mView.findViewById(R.id.post_title_tv);
postTitleTv.setText(title);
}
public void setRequesterName(String name){
requesterNameTv = (TextView) mView.findViewById(R.id.requester_name_tv);
requesterNameTv.setText(name);
}
public void setRequesterPropic(Context context, String path){
requesterPropic = (ImageView) mView.findViewById(R.id.requester_propic_imgview);
Picasso.with(context).load(path).into(requesterPropic);
}
public void setTimestamp(String timestamp){
timestampTv = (TextView) mView.findViewById(R.id.timestamp_tv);
timestampTv.setText(timestamp);
}
}
}
火力地堡數據庫:
請幫幫我。提前致謝。
代碼在哪裏,它負責從Firebase獲取數據 – Rahul
更新您的適配器代碼 –
@Rahul在DashboardFragment.java中,onStart()方法中有一個適配器。 –