从嵌套的onDataChange修改列表

时间:2018-03-10 17:04:55

标签: android firebase firebase-realtime-database

我正在尝试将数据(字符串名称)添加到嵌套的onDataChange中的列表中。 在外部onDataChange中,数据被添加到timeIntervals arraylist中。在嵌套的onDataChange中,我确实获得了String name值,但是它没有被插入到timeIntervals arraylist中,因此没有显示在List中。我不确定为什么会这样。对不好的代码感到抱歉。截止日期临近。

  mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_id).child("week").child(day);
    mUserDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            daySchedule = (ArrayList<Long>)dataSnapshot.getValue();
            Log.d("daySchedule", "onDataChange: "+daySchedule.toString());
            int k = 0;
            startIndexes = new ArrayList<Integer>();
            endIndexes = new ArrayList<Integer>();
            timeIntervals = new ArrayList<String>();
            apptindex = new ArrayList<Integer>();
            apptnumber = new ArrayList<Long>();
            for (int i = 0; i < daySchedule.size(); i++) {
                //starthour = daySchedule.get(i);
                if (daySchedule.get(i) == 1L) {
                    //Log.d("checkone", "onDataChange: ");
                    startIndexes.add(i);
                    k = i;
                    while (k <= 15 && daySchedule.get(k) == 1L) {
                        k = k + 1;
                    }
                    endIndexes.add(k-1);
                    i = k;
                } else if (daySchedule.get(i) != 0) {
                    //Toast.makeText(DayAppointmentsActivity.this, "hi", Toast.LENGTH_SHORT).show();
                    apptindex.add(i);
                    apptnumber.add(daySchedule.get(i));
                }
            }
            Log.d("startindexes", "onDataChange: "+startIndexes.toString());
            Log.d("endindexes", "onDataChange: "+endIndexes.toString());
            int starthour = 0;
            int startmin = 0;
            int endhour = 0;
            int endmin = 0;
            for (int j = 0; j < startIndexes.size(); j++) {
                starthour = startIndexes.get(j);
                int check1 = starthour;
                if (starthour == 0) {
                    starthour = starthour + 9;
                } else if (starthour % 2 == 0) {
                    starthour = 9 + (starthour / 2);
                } else {
                    starthour = 9 + (starthour / 2);
                    startmin = 30;
                }
                endhour = endIndexes.get(j);
                int check2 = endhour;
                if (endhour == 0) {
                    endhour = endhour + 9;
                } else if (endhour % 2 == 0) {
                    endhour = 9 + (endhour / 2);
                } else {
                    endhour = 9 + (endhour / 2);
                    endmin = 30;
                }
                if (check1 == check2) {
                    if (check1 == 0) {
                        endmin = 30;
                    } else if (check2 % 2 == 0) {
                        endmin = 30;
                    } else {
                        endmin = 0;
                        endhour = endhour + 1;
                    }
                }
                Log.d("startingtime", "onDataChange: "+starthour + " " +startmin);
                Log.d("endingtime", "onDataChange: "+endhour + " " +endmin);
                String time = "Free: " + starthour + ":" + startmin + " to " + endhour + ":" + endmin;
                timeIntervals.add(time);
                //Toast.makeText(DayAppointmentsActivity.this, time, Toast.LENGTH_SHORT).show();
                startmin = 0;
                endmin = 0;
            }
            for (int j = 0; j < apptindex.size(); j++) {
                int index = apptindex.get(j);
                long apptnum = apptnumber.get(j);
                String appnumString = Long.toString(apptnum);
                if (daySchedule != null) {
                    mApptDatabase = FirebaseDatabase.getInstance().getReference().child("Appointments").child(appnumString);
                    mApptDatabase.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            String name = dataSnapshot.child("targetName").getValue().toString();
                            Toast.makeText(DayAppointmentsActivity.this, name, Toast.LENGTH_SHORT).show();
                            timeIntervals.add(name);
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });
                }

            }
            Log.d("timeintervals", "onDataChange: "+timeIntervals.toString());
            listAdapter = new ArrayAdapter<String>(DayAppointmentsActivity.this,android.R.layout.simple_list_item_1,timeIntervals);
            listView.setAdapter(listAdapter);
        }

1 个答案:

答案 0 :(得分:0)

欢迎使用异步数据加载,它始终向您发送消息。与您的想法相反,数据 会被添加到timeIntervals。它只是在之后添加>进行打印。

最简单的方法是添加一些放置好的日志语句:

Log.d("timeintervals", "Before starting load additional data");
for (int j = 0; j < apptindex.size(); j++) {
    int index = apptindex.get(j);
    long apptnum = apptnumber.get(j);
    String appnumString = Long.toString(apptnum);
    if (daySchedule != null) {
        mApptDatabase = FirebaseDatabase.getInstance().getReference().child("Appointments").child(appnumString);
        mApptDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.d("timeintervals": "Additional data loaded");
                String name = dataSnapshot.child("targetName").getValue().toString();
                timeIntervals.add(name);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                throw databaseError.toException(); // don't ignore errors
            }
        });
    }

}
Log.d("timeintervals", "After starting load additional data");

运行此代码时输出为:

  

开始加载其他数据之前

     

开始加载其他数据后

     

加载了其他数据

     

加载了其他数据

     

...

这可能不是您预期的顺序。但它完全解释了为什么timeIntervals在打印时没有显示附加信息:数据尚未加载。

这是因为Firebase异步加载数据库中的数据,并且在加载数据时不会阻止您的应用。

处理异步数据的典型解决方案是将需要数据的代码移动到数据加载时触发的onDataChange。例如。如果您将timeIntervals的日志记录移动到onDataChange,则每次加载其中一个名称时都会记录这些时间间隔。

要了解更多相关信息,我建议您阅读以下内容: