【问题标题】:Android Studio Application display problemAndroid Studio 应用显示问题
【发布时间】:2019-07-10 08:24:01
【问题描述】:

我正在制作一个应用程序,它从数据库中获取特定数据到应用程序并在回收站视图中显示它,但我有一个我不知道如何解决的问题,我很确定它必须使用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的路径

编辑1: 为 Recycler Adapter 和 HomeFragment 上的代码添加了 pastebin

【问题讨论】:

  • 有什么问题;让代码运行;什么是 logcat 堆栈跟踪
  • 当您运行应用程序时,它通常会在 Logcat 中显示在窗口底部
  • 我认为你的问题是你需要在事件完成后设置 blog_list 然后调用 notifydatasetchangesd()
  • 是的,但为此您需要添加 Log.d("TAG", "info");在您的代码中
  • logcat 将有助于了解是否添加了博客

标签: android firebase github


【解决方案1】:

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

【讨论】:

  • 在更新适配器之前添加一个日志并检查列表的大小。这个日志会出现在logcat中,找不到就用tag搜索。在解决方案中添加。
  • 如果去掉if条件呢? if (doc.getType() == DocumentChange.Type.ADDED).
  • 似乎您实际上没有从Firestore 获取文档。 Firestore 有文件吗?
  • 您能否从 Firebase 控制台检查您是否在 Firestore 中的 articles 集合中有文档?
  • 但这看起来像Realtime Database,而您在代码中使用Firestore,它们是不同的。
猜你喜欢
  • 1970-01-01
  • 2022-11-12
  • 2021-04-17
  • 2015-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多