Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我能';从firebase android查询项目后,不要将其添加到HashMap_Java_Android_Google Cloud Firestore - Fatal编程技术网

Java 我能';从firebase android查询项目后,不要将其添加到HashMap

Java 我能';从firebase android查询项目后,不要将其添加到HashMap,java,android,google-cloud-firestore,Java,Android,Google Cloud Firestore,我的目标是显示针对特定主题发布的最后一篇文章的作者和日期 我有两个收藏,论坛主题和论坛帖子。 我迭代了所有论坛主题,获得了所需的数据,并将其放入HashMap topicData中,然后将HashMap存储在ArrayList中,并通过ListView显示,这一切都很好 但是当我想通过论坛帖子(这个查询嵌套在论坛主题查询中)获取最新的帖子作者和日期时,我得到了数据,但无法将其放入HashMap topicData中。 这个查询还可以,因为它得到了日志显示的最后一个作者和帖子,但它们没有被放入Ha

我的目标是显示针对特定主题发布的最后一篇文章的作者和日期

我有两个收藏,论坛主题和论坛帖子。 我迭代了所有论坛主题,获得了所需的数据,并将其放入HashMap topicData中,然后将HashMap存储在ArrayList中,并通过ListView显示,这一切都很好

但是当我想通过论坛帖子(这个查询嵌套在论坛主题查询中)获取最新的帖子作者和日期时,我得到了数据,但无法将其放入HashMap topicData中。 这个查询还可以,因为它得到了日志显示的最后一个作者和帖子,但它们没有被放入HashMap中,我不知道问题出在哪里

在代码中,有一条注释指向确实获取数据但不向hashmap添加数据的查询

下面是代码

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_forum);
    addTopic = findViewById(R.id.addNewTopicBtn);
    fStore = FirebaseFirestore.getInstance();
    empty = findViewById(R.id.empty);


    final ArrayList<Map> forumTopics = new ArrayList<>();

    final ListAdapter forumAdapter = new ForumAdapter(this, forumTopics);
    ListView forumListView = findViewById(R.id.ForumListView);
    forumListView.setEmptyView(empty);
    forumListView.setAdapter(forumAdapter);


    fStore.collection("forum_topics")
            .orderBy("date_published", Query.Direction.ASCENDING)
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            final Map<String, Object> topicData = new HashMap<>();
                            // getting topic name and putting it to the topicData HashMap
                            topicData.put("topic_name", document.getId());
                            // getting topic author and putting it to the topicData HashMap
                            topicData.put("author", document.getString("author"));
                            // getting the date_published timestamp
                            Date date_published = document.getTimestamp("date_published").toDate();
                            // formatting the date to the desired 24hr format and putting it into the HashMap
                            topicData.put("date_published", DateFormat.format("dd-MM-yyyy hh:mm:ss", date_published).toString());
                            // getting the post_num and putting it to the topicData HashMap
                            topicData.put("post_num", document.get("post_num").toString());
                            Log.d(TAG, "Got the topic with name: " + document.getId());

// below is the problematic query that doesn't add data to HashMap
                            fStore.collection("forum_posts")
                                    .whereEqualTo("topic_name", document.getId())
                                    .orderBy("date_published", Query.Direction.DESCENDING)
                                    .limit(1)
                                    .get()
                                    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                        @Override
                                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                            if (task.isSuccessful()) {
                                                for (QueryDocumentSnapshot post : task.getResult()) {
                                                    Log.d(TAG, "got the last author " + post.getString("author"));
                                                    topicData.put("last_post_author", post.getString("author"));
                                                    Date date_published = post.getTimestamp("date_published").toDate();
                                                    String last_date_published = DateFormat.format("dd-MM-yyyy hh:mm:ss", date_published).toString();
                                                    topicData.put("last_post_published", last_date_published);
                                                    Log.d(TAG, "got the last post " + last_date_published);
                                                }
                                            } else {
                                                Log.d(TAG, "Error getting documents: ", task.getException());
                                            }
                                        }
                                    });

                            forumTopics.add(topicData);
                        }
                        // notifying the forumAdapter with data change
                        ((ArrayAdapter) forumAdapter).notifyDataSetChanged();
                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                }
            });

    forumListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Map topicData = (Map) parent.getItemAtPosition(position);
                    String topic = (String) topicData.get("topic_name");
                    Intent intent = new Intent(Forum.this, ForumTopic.class);
                    intent.putExtra("topic_name", topic);
                    startActivity(intent);
                    finish();
                }
            }
    );

    addTopic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(Forum.this, ForumPostTopic.class));
        }
    });

}
firebase的屏幕截图:

论坛主题:

论坛帖子:从这里我得到了红色的数据,但无法将其放入HashMap

来自设备的屏幕截图:


在第二次查询中从onComplete获得回调后,我通过通知forumAdapter成功地解决了这个问题

在调试应用程序时,我看到第二个查询中的代码是在第一个查询完成后执行的。看起来查询被搁置了

所以数据实际上被添加到了HashMap中,但适配器不知道添加了新数据,所以它必须被通知两次,我认为在主查询之后就足够了,显然每次数据集更改时都必须通知它,这是一个非常有价值的教训,我认为新的解决方案很简单

代码如下(我只发布了我认为不起作用的查询的代码):

fStore.collection(“论坛帖子”)
.whereEqualTo(“主题名称”,document.getId())
.orderBy(“发布日期”,Query.Direction.DESCENDING)
.限额(1)
.get()
.addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
对于(QueryDocumentSnapshot帖子:task.getResult()){
Log.d(标记“获得最后一位作者”+post.getString(“作者”);
topicData.put(“最后一位作者”,post.getString(“作者”);
Date Date_published=post.getTimestamp(“Date_published”).toDate();
字符串last_date_published=DateFormat.format(“dd-MM-yyy-hh:MM:ss”,date_published).toString();
topicData.put(“上次发布”,上次发布日期);
Log.d(标记“获得最后一篇文章”+最后发布日期);
}
}否则{
Log.d(标记“获取文档时出错:”,task.getException());
}
//用数据更改通知forumAdapter
((ArrayAdapter)forumAdapter).notifyDataSetChanged();
}
});

请将您的数据库结构添加为屏幕截图,并指出您想要获取的确切数据。我使用Firebase firestore,我获取数据是因为日志显示,问题是,尽管数据只是放在HashMap中不起作用,因为您记录时数据就在那里。我将添加屏幕截图。
D/TAG: Got the topic with name: Some new topic in here
D/TAG: Got the topic with name: This is a new topic
D/TAG: Got the topic with name: 123
D/TAG: Got the topic with name: new topic
I/zygote: Do full code cache collection, code=124KB, data=79KB
I/zygote: After code cache collection, code=123KB, data=48KB
I/zygote: Do partial code cache collection, code=123KB, data=47KB
I/zygote: After code cache collection, code=123KB, data=47KB
    Increasing code cache capacity to 512KB
D/TAG: got the last author Jo Do
D/TAG: got the last post 26-02-2020 10:42:46
    got the last author Jo Do
D/TAG: got the last post 26-02-2020 02:29:16
    got the last author Jo Do
D/TAG: got the last post 26-02-2020 11:01:25
D/TAG: got the last author Jo Do
D/TAG: got the last post 28-02-2020 11:19:34
fStore.collection("forum_posts")
        .whereEqualTo("topic_name", document.getId())
        .orderBy("date_published", Query.Direction.DESCENDING)
        .limit(1)
        .get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot post : task.getResult()) {
                        Log.d(TAG, "got the last author " + post.getString("author"));
                        topicData.put("last_post_author", post.getString("author"));
                        Date date_published = post.getTimestamp("date_published").toDate();
                        String last_date_published = DateFormat.format("dd-MM-yyyy hh:mm:ss", date_published).toString();
                        topicData.put("last_post_published", last_date_published);
                        Log.d(TAG, "got the last post " + last_date_published);
                    }
                } else {
                    Log.d(TAG, "Error getting documents: ", task.getException());
                }
                // notifying the forumAdapter with data change
                ((ArrayAdapter) forumAdapter).notifyDataSetChanged();
            }
        });