【问题标题】:How to get the selected index document from the firestore?如何从 Firestore 中获取选定的索引文档?
【发布时间】:2020-09-02 12:25:16
【问题描述】:

我正在尝试从列表中获取所选项目索引,并且数据已存储在 firestore 中,我尝试的是,

   db.collection("Musicians").orderBy("DocumentID", Query.Direction.ASCENDING).limit(30).get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()){

                List<DocumentSnapshot> snapshotslist= Objects.requireNonNull(task.getResult()).getDocuments();
               (DocumentSnapshot document : snapshotslist) {

              //--- This way is not working----------------------------------//
               int index = snapshotslist.indexOf("Flkt8Fb0gWvO9xaUYR0V");//This method always return -1


              //----This way is working--------------------------------//
               int index = snapshotslist.indexOf(document);// This method is working and it gives all index of the list

但我只需要获取我输入的文档的索引。例如,


  • Flkt8Fb0gWvO9xaUYR0V
  • BpJQAYm7Y49XA2IZMGhE
  • LTy4KuRpXTc5e7FK2I9o
  • bxzM9drC9DWDieWEbFLl

我想得到输出为 2 的“LTy4KuRpXTc5e7FK2I9o”的索引。

【问题讨论】:

    标签: java firebase google-cloud-firestore


    【解决方案1】:

    如果我理解正确,您想根据 ID 在列表中查找文档的索引。

    在这种情况下,你可以这样做:

    List<DocumentSnapshot> snapshotslist= Objects.requireNonNull(task.getResult()).getDocuments();
    int index = 0;
    for (DocumentSnapshot document : snapshotslist) {
      if (document.getId().equals("Flkt8Fb0gWvO9xaUYR0V")) {
        break;
      }
      index++;
    }
    

    【讨论】:

    • 是的,你是对的,我需要根据文档的 ID 在列表中获取文档的索引,并且我想将索引设置为 textview。
    猜你喜欢
    • 2022-01-20
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 2020-12-05
    • 2021-10-22
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多