我正在制作一个应用程序,它从数据库中获取特定数据到应用程序并将其显示在回收站视图中,但我有一个我不知道如何解决的问题,我很确定它与HomeFragment.java
. 请检查脚本末尾附近的多行注释。我将添加 GitHub Repo,因为我认为这不是单文件错误。
很抱歉你可能会因为如此含糊而提前遇到的痛苦和折磨,但我不知道该怎么说更好。
BlogRecyclerAdapter.Java:https://pastebin.com/39G2mEW5
HomeFragment.Java:https://pastebin.com/YmKs5QUJ
谢谢你!
回购:https : //github.com/hjdaboss123/BlindNews
路径 HomeFragment.javaBlind:News/app/src/main/java/com/blindnews/kimh2/blindnews
EDIT1:为 Recycler Adapter 和 HomeFragment 上的代码添加了 pastebin
这不是问题的完整解决方案,但在评论中我们发现了问题,即 OPFirestore
用于从Realtime database
.
在片段中改变这个 -
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
blog_list = new ArrayList<>();
blog_list_view = view.findViewById(R.id.blog_list_view);
blogRecyclerAdapter = new BlogRecyclerAdapter(blog_list);
blog_list_view.setLayoutManager(new LinearLayoutManager(container.getContext()));
blog_list_view.setAdapter(blogRecyclerAdapter);
firebaseFirestore = FirebaseFirestore.getInstance();
firebaseFirestore.collection("articles").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
if (doc.getType() == DocumentChange.Type.ADDED) {
BlogPost blogPost = doc.getDocument().toObject(BlogPost.class);
blog_list.add(blogPost);
}
}
Log.d("HomeFragment", "onCreateView: " + blog_list.size());
//Send updated list to adapter.
blogRecyclerAdapter.updatePosts(blog_list);
}
});
return view;
}
处理适配器内的更新列表 -
public void updatePosts(List<BlogPost> blogPostList) {
//if you want to update the whole list. If you want to append, List has addAll method I think and use it with notifyItemRangeInserted for better performance.
this.blog_list = blogPostList;
notifyDataSetChanged();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句