I am trying to add data (The String name) to the list in the nested onDataChange. In the outer onDataChange, the data is being added to the timeIntervals arraylist. In the nested onDataChange, I do get the String name value, however, it is not being inserted into the the timeIntervals arraylist and thus does not show up in the List. I am not sure why this happens. Sorry about the bad code. the deadline is approaching.
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);
}