ユーザログイン直後にRecyclerViewを使用して、Firebaseデータベースの投稿をダッシュボードに表示しようとしています。私は何がうまくいかないのか分からない。 RecyclerViewにデータが入力されていません。 1つのアイテムでも表示されません。私はGoogleでたくさんの検索をしましたが、解決策は動作していないようです。フラグメントの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データベース:
私を助けてください。前もって感謝します。
Firebaseからデータをフェッチするコードはどこにありますか – Rahul
アダプタコードも更新してください –
@Rahul DashboardFragment.javaには、onStart()メソッドのアダプタがあります。 –