I need to get the count of records for the current day, from the database. The entries in the Firebase Realtime are in timestamp, I parse them using SimpleDateFormat.
But I could not get the count of records, for example, for today's date. Tell me what to do? Thanks
IMAGE RECORDS IN FIREBASE REALTIME
private void fetchDataWaterCount() {
currDate = null;
String uid = firebaseAuth.getUid();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
currDate = LocalDate.now().toString();
}
waterRef = FirebaseDatabase.getInstance().getReference("Users").child(uid).child("body").child("water");
waterRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
for (DataSnapshot ds : snapshot.getChildren()) {
waterModel = ds.getValue(WaterModel.class);
waterArray.add(waterModel);
waterModel.setWaterModelList(waterArray);
ArrayList listDate = new ArrayList();
for (int i = 0; i < waterModel.getWaterModelList().size(); i ) {
long dateList = waterModel.getWaterModelList().get(i).getDate();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
String dateFormat = formatter.format(new Date(dateList));
listDate.add(dateFormat);
if(listDate.contains(currDate)){
int water_count = (int) listDate.size();
drawable = getActivity().getDrawable(R.drawable.circut_progress_bar);
progressBar.setProgress(water_count);
progressBar.setMax(12);
progressBar.setSecondaryProgress(water_count);
progressBar.setProgressDrawable(drawable);
textViewWaterCount.setText(String.valueOf(water_count));
if (water_count >= 12) {
textViewWaterCount.setText("Done");
textViewWaterCount.setTextColor(getResources().getColor(R.color.colorGreen_900));
drawable = getActivity().getDrawable(R.drawable.circut_progressbar_green);
progressBar.setProgressDrawable(drawable);
}
}
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
CodePudding user response:
If you want to know the amount of WaterModels
that match a specific date then you need to count them as they appear.
You are currently adding every date to the list regardless if it matches the currDate
, so your count is the total dates not the specific amount of specific dates.
//Add this
int dateCount = 0;
for (int i = 0; i < waterModel.getWaterModelList().size(); i ) {
long dateList = waterModel.getWaterModelList().get(i).getDate();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
String dateFormat = formatter.format(new Date(dateList));
listDate.add(dateFormat);
//Add this
if(dateFormat == currDate){
dateCount ;
}
//I don't know what your doing here but this will return true
//if there is at least 1 match. You can't use this to track how many dates
//match
if (listDate.contains(currDate)) {
int water_count = (int) listDate.size();
drawable = getActivity().getDrawable(R.drawable.circut_progress_bar);
progressBar.setProgress(water_count);
progressBar.setMax(12);
progressBar.setSecondaryProgress(water_count);
progressBar.setProgressDrawable(drawable);
textViewWaterCount.setText(String.valueOf(water_count));
if (water_count >= 12) {
textViewWaterCount.setText("Done");
textViewWaterCount.setTextColor(getResources().getColor(R.color.colorGreen_900));
drawable = getActivity().getDrawable(R.drawable.circut_progressbar_green);
progressBar.setProgressDrawable(drawable);
}
}
}
dateCount
will contain the total amount of Dates that match the currDate
.