android - Android Studio 应用显示问题
问题描述
我正在制作一个应用程序,它从数据库中获取特定数据到应用程序并在回收器视图中显示它,但是我有一个问题,我不知道如何解决,我很确定它与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();
}
推荐阅读
- c# - Csharp 回声消除 Speex
- java - jenkins 中的 Jacoco 覆盖率比较
- unit-testing - 这样的单元测试有什么好处?
- javascript - 如何将 SVG 元素插入 DOM?
- c# - Selenium 清除一个文本框,然后重新填充它
- python - 将数据转换为实际时间戳 - 如何添加正确的单位?
- react-native - 如何调试 react-native-reanimated 2?
- mysql - MySQL GROUP_CONCAT 按另一列排序
- visual-studio-code - 使用 Cygwin GDB 在 VS 代码中进行 C++ 调试:如何查看局部变量的值?
- image - 羽毛笔文本编辑器调整图像大小