从Firebase Firestore检索数据

时间:2018-09-20 06:19:51

标签: android firebase

请,有人帮助我并节省了我从Firebase Firestore检索数据的时间,但是数据未显示在适配器中。从Firestore检索数据成功,我检查了Array列表,该列表不为空,但未显示数据。请帮助

这是我的代码

Fragment.java

public class HomeFragment extends Fragment {


private RecyclerView blog_recycler_view;

List<BlogPostModel> blogPostModelList ;
FirebaseFirestore firebaseFirestore;
private  BlogPostRecyclerAdapter blogPostRecyclerAdapter;


public HomeFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    blog_recycler_view = view.findViewById(R.id.blog_recycler_view);



    blogPostModelList = new ArrayList<>();
    firebaseFirestore = FirebaseFirestore.getInstance();

    blogPostRecyclerAdapter = new BlogPostRecyclerAdapter(getContext(),blogPostModelList);
    blog_recycler_view.setLayoutManager(new LinearLayoutManager(getContext()));
    blog_recycler_view.setAdapter(blogPostRecyclerAdapter);




    //get the data from fire base fire store and then set the data in blog post data model
    //then the model data set in the list
    firebaseFirestore.collection("Posts").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

            for (DocumentChange documentChange : documentSnapshots.getDocumentChanges())
            {
                if (documentChange.getType() == DocumentChange.Type.ADDED)
                {
                    //receive post for every time
                    BlogPostModel blogPostModel = documentChange.getDocument().toObject(BlogPostModel.class);
                    //add the data in the list
                    blogPostModelList.add(blogPostModel);

                    blogPostRecyclerAdapter.notifyDataSetChanged();



                    Toast.makeText(getActivity(), "", Toast.LENGTH_SHORT).show();

                }
            }
        }

    });

    return view;
  }

}

“自定义适配器”是

public class BlogPostRecyclerAdapter extends RecyclerView.Adapter<BlogPostRecyclerAdapter.ViewHolder> {

Context context;
public List<BlogPostModel> blogList  ;

public BlogPostRecyclerAdapter(Context context, List<BlogPostModel> blogList) {
    this.context = context;
    this.blogList = blogList;
}

@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_list_item,null);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    String description_data = blogList.get(position).getDiscription();
    holder.descTextView.setText(description_data);

}

@Override
public int getItemCount() {
    return blogList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    private TextView descTextView;
    public ViewHolder(View itemView) {
        super(itemView);

        descTextView = itemView.findViewById(R.id.blog_description);


      }

   }
}

1 个答案:

答案 0 :(得分:1)

您正在尝试从作为Firebase响应的回调的线程更新UI。 您应该调用方法notifyDataSetChanged();。来自UI线程。

尝试一下:

getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
         blogPostRecyclerAdapter.notifyDataSetChanged();
    }
});