我正在开发一个与 firebase 集成的计步器的个人项目,但我对日期不太满意。不知道您是否可以提供帮助...我要实现的目标是当用户单击当天活动的保存/更新时,它应该更新当天的同一特定文档。第二天打开应用程序时,它应该在其集合下的 firestore 中创建另一个文档,并且当用户单击更新时……相同的过程继续进行。但是当我比较代码中的两个字符串时,我没有得到预期的实际结果。当我运行应用程序并单击更新时。它不断在同一集合下添加具有相同日期的新文档。有什么办法可以解决这个问题?在此先感谢 查看图片
mSaveDataToFireStore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String stepCountNum = mTV_StepCounter.getText().toString();
String stepCountMeter = mTV_StepCounter_meter.getText().toString();
String stepCalories = mCalories_Burn.getText().toString();
String stepDuration = mCounter_Duration.getText().toString();
DateFormat dateFormat = new SimpleDateFormat("E, MMM dd yyyy");
Date date = new Date();
saveCurrentDate = dateFormat.format(date).toString();
Calendar callForDate = Calendar.getInstance();
SimpleDateFormat currentDay = new SimpleDateFormat("E");
saveCurrentDay = currentDay.format(callForDate.getTime());
SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm:ss a");
saveCurrentTime = currentTime.format(callForDate.getTime());
FirebaseUser AppUser = FirebaseAuth.getInstance().getCurrentUser();
if (AppUser != null) {
USER_ID = FirebaseAuth.getInstance().getCurrentUser().getUid();
STEP_COUNTER = FirebaseAuth.getInstance().getCurrentUser().getUid();
CollectionReference colRef = FirebaseFirestore.getInstance()
.collection("REGISTERED USERS").document(USER_ID)
.collection("STEP COUNTER");
colRef.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
StepCounter_Model stepCountUpdate = new StepCounter_Model(saveCurrentDate, saveCurrentDay, saveCurrentTime, stepCountNum,
stepCountMeter, stepCalories, stepDuration);
if (!queryDocumentSnapshots.isEmpty()) {
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
String currentDayy = documentSnapshot.getString("mStepDay");
String currentDatee = documentSnapshot.getString("mStepDate");
StepCounter_Model StepCountData = documentSnapshot.toObject(StepCounter_Model.class);
DateFormat dateFormat = new SimpleDateFormat("E, MMM dd yyyy");
Date date = new Date();
String getTodayDate = dateFormat.format(date).toString();
if (currentDatee.compareTo(getTodayDate) == 0) {
//currentDatee.equals(getTodayDate) && currentDatee.matches(getTodayDate) && !currentDatee.isEmpty()
Toast.makeText(StepTrackerActivity.this, "Data updated successfully", Toast.LENGTH_SHORT).show();
StepCountData.setmStepID(documentSnapshot.getId());
String documentID = StepCountData.getmStepID();
Map<String, Object> saveInfo = new HashMap<>();
saveInfo.put("mStepDate", stepCountUpdate.mStepDate);
saveInfo.put("mStepDay", stepCountUpdate.mStepDay);
saveInfo.put("mStepTime", stepCountUpdate.mStepTime);
saveInfo.put("mStepCount", stepCountUpdate.mStepCount);
saveInfo.put("mStepCountMeter", stepCountUpdate.mStepCountMeter);
saveInfo.put("mStepCalories", stepCountUpdate.mStepCalories);
saveInfo.put("mStepDuration", stepCountUpdate.mStepDuration);
colRef.document(documentID).update(saveInfo);
return;
}
if (currentDatee.compareTo(getTodayDate) < 0 || currentDatee.compareTo(getTodayDate) > 0) {
//!currentDatee.equals(getTodayDate) && !currentDatee.matches(getTodayDate)
//resetCounter();
addTo_FireStoreDB(saveCurrentDate, saveCurrentDay, saveCurrentTime, stepCountNum,
stepCountMeter, stepCalories, stepDuration);
return;
}
}
} else {
addTo_FireStoreDB(saveCurrentDate, saveCurrentDay, saveCurrentTime, stepCountNum,
stepCountMeter, stepCalories, stepDuration);
}
}
});
}
}
});