首页 > 解决方案 > 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

标签: androidfirebasegithub

解决方案


这不是问题的完整解决方案,但在评论中我们发现了问题,即 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();
}

推荐阅读