简体   繁体   English

Firebase Cloud Firestore:查询未更新 Android 中的 ArrayList

[英]Firebase Cloud Firestore: Query isn't updating ArrayList in Android

I am using the following query:我正在使用以下查询:

db.collection("Example")
                .whereEqualTo("UserId", currentUser.getUid())
                .orderBy("timestamp", Query.Direction.ASCENDING)
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()){
                            for (QueryDocumentSnapshot document : task.getResult()){
                                Log.i(TAG, "onComplete: " + document.get("UserId") + " => " + document.get("userText"));
                                userIdArrayList.add(document.get("UserId").toString());
                                userTextArrayList.add(document.get("userText").toString());

                            }
                        }else{
                            Log.i(TAG, "onComplete: Error getting documents", task.getException());
                        }
                    }
                });

As the query is iterating through results, I would like it to add the results to an array.当查询遍历结果时,我希望它将结果添加到数组中。 However, when I run the code and attempt to print the content of the array list, I am getting and IndexOutOfBounds exception despite me looking at userTextArrayList.get(0);但是,当我运行代码并尝试打印数组列表的内容时,尽管我查看了userTextArrayList.get(0);但我还是收到了IndexOutOfBounds异常userTextArrayList.get(0); which should contain a result.其中应该包含一个结果。 I have added the line Log.i to check if the code is running successfully and I can see in the Logcat that the data is being pulled from the database.我添加了一行Log.i来检查代码是否成功运行,我可以在 Logcat 中看到正在从数据库中提取数据。 For some reason this code isn't adding the results to the ArrayList.出于某种原因,此代码未将结果添加到 ArrayList。

I'm not sure what else to try and I am unable to find anywhere in the documentation that can assist me with where I have potentially gone wrong.我不知道还有什么可以尝试的,而且我无法在文档中找到任何可以帮助我解决可能出错的地方。

Please feel free to point me towards the documentation that can assist with the problem if providing the answer is too much bother.如果提供答案太麻烦,请随时向我指出可以帮助解决问题的文档。 I appreciate any guidance anyone can provide.我感谢任何人可以提供的任何指导。

Thanks!谢谢!

Edit: I am getting an error on the following lines of code:编辑:我在以下代码行中收到错误消息:

  1. userIdArrayList.add(document.get("UserId").toString());
  2. userTextArrayList.add(document.get("userText").toString())

For some reason these lines aren't adding the data to the array and when I attempt to print the array later in the code (ie Log.i(TAG, "User Text => userTextArrayList.get(0).toString()); ), I am recieving and error for IndexOutOfBounds - apparently the ArrayList does not contain data despite the above code adding data to the ArrayLists.出于某种原因,这些行没有将数据添加到数组中,当我稍后在代码中尝试打印数组时(即Log.i(TAG, "User Text => userTextArrayList.get(0).toString()); ),我收到了IndexOutOfBounds错误信息 - 尽管上面的代码向 ArrayLists 添加了数据,但显然 ArrayList 不包含数据。

You need to understand that this query is asynchronous and the results might not have yet been added to userTextArrayList when you execute this log statement Log.i(TAG, "User Text => userTextArrayList.get(0).toString());您需要了解此查询是异步的,并且当您执行此日志语句Log.i(TAG, "User Text => userTextArrayList.get(0).toString());时,结果可能尚未添加到userTextArrayList Log.i(TAG, "User Text => userTextArrayList.get(0).toString());

This log statement needs to be in onComplete() as below in order for it to work:此日志语句需要在onComplete() ,如下所示,才能使其工作:

@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
    if (task.isSuccessful()) {
        for (QueryDocumentSnapshot document : task.getResult()) {
            Log.i(TAG, "onComplete: " + document.get("UserId") + " => " + document.get("userText"));            
            userIdArrayList.add(document.get("UserId").toString());      
            userTextArrayList.add(document.get("userText").toString());
        }
        yourButton.setEnabled(true);
    } else {
        Log.i(TAG, "onComplete: Error getting documents", task.getException());
        yourButton.setEnabled(false);
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM