【问题标题】:ArrayList<Object> copied from another ArrayList is empty even if there is data [duplicate]即使有数据,从另一个 ArrayList 复制的 ArrayList<Object> 也是空的 [重复]
【发布时间】:2019-11-20 13:37:58
【问题描述】:

我通过 Model 类将项目添加到列表中,然后将这些项目添加到 ArrayList&lt;Object&gt;。但是,即使commentsList.size() &gt; 1,我的大小总是为零。

我尝试将List 转换为ArrayList 并尝试添加项目。但ArrayList&lt;Object&gt; 的大小始终为 0。

ArrayList<Comments> commentsList = new ArrayList<>(Arrays.asList(new Comments("username", "time", "date")));

这是我正在使用的函数。

private ArrayList<Object> getObject() {

    if (getComments() != null && getComments().size()>=1) {
        objects.add(getComments().get(0));
    }

    return objects;
}

public static ArrayList<Comments> getComments() {
    ArrayList<Comments> commentsList = new ArrayList<>();

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Comments");

    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            commentsList.clear();

            for (DataSnapshot shot : snapshot1.getChildren()) {
                 Comments comments = shot.getValue(Comments.class);
                 commentsList.add(comments);                          
           }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

    return commentsList;
}

【问题讨论】:

  • 向 ArrayList 添加列表并没有什么特别之处。如果将列表添加到 ArrayList,它的大小会增加。如果情况似乎并非如此,那么您要么在检查之前清除它,要么不添加任何东西。请修正您的格式并显示minimal reproducible example

标签: java android asynchronous arraylist firebase-realtime-database


【解决方案1】:

getComments 函数中,您正在执行一个异步操作,该操作从您的firebase 数据库中检索数据。因此,您的commentsList 中实际上没有任何内容。当然,该函数只是用零个元素初始化一个新的ArrayList,并创建一个等待在您的应用程序中接收数据的ValueEventListener。但是,此操作是异步的(在单独的线程中运行),因此在创建 ValueEventListener 后,该函数将立即返回空列表。因此,当您尝试在 getObject 函数中构建 ArrayList&lt;Object&gt; 时,您也会得到一个空列表。

我建议编写另一个函数,在从您的 firebase 数据库接收数据后调用 onDataChange 函数时将调用该函数。例如,在与以下相同的类中编写一个函数。

void nowCreateArrayListOfObjects() {
    // TODO: Call the getObject function here
}

现在从您的onDataChange 函数调用此函数,如下所示。

public static ArrayList<Comments> getComments() {
    ArrayList<Comments> commentsList = new ArrayList<>();

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Comments");

    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            commentsList.clear();

            for (DataSnapshot shot : snapshot1.getChildren()) {
                Comments comments = shot.getValue(Comments.class);
                commentsList.add(comments);
            }

            // Invoke the function here to get the values now
            nowCreateArrayListOfObjects();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

    return commentsList;
}

希望有帮助!

【讨论】:

  • 虽然这不适用于静态,但我调整了这个想法并将课程设为私有空隙,现在一切都很顺利!
猜你喜欢
  • 1970-01-01
  • 2014-06-15
  • 1970-01-01
  • 2018-08-19
  • 2020-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-28
相关资源
最近更新 更多