others - android java如何按日期筛选来自多个文档的数据Firestore

我已经把一些测试数据放到Firestore来测试我的应用程序,我想查询当前登录用户的数据,然后只获取今天保存到数据库中的数据。我的代码现在看起来像这样:


package com.example.wht;



import androidx.annotation.NonNull;


import androidx.appcompat.app.AppCompatActivity;


import androidx.appcompat.widget.Toolbar;


import androidx.cardview.widget.CardView;



import android.content.Intent;


import android.os.Bundle;


import android.text.format.DateFormat;


import android.util.Log;


import android.view.Menu;


import android.view.MenuItem;


import android.view.View;


import android.widget.ImageButton;


import android.widget.TextView;


import android.widget.Toast;



import com.example.wht.model.WhtItem;


import com.example.wht.ui.WhtRecyclerAdapter;


import com.example.wht.util.WhtApi;


import com.google.android.gms.tasks.OnFailureListener;


import com.google.android.gms.tasks.OnSuccessListener;


import com.google.firebase.auth.FirebaseAuth;


import com.google.firebase.auth.FirebaseUser;


import com.google.firebase.firestore.CollectionReference;


import com.google.firebase.firestore.FirebaseFirestore;


import com.google.firebase.firestore.Query;


import com.google.firebase.firestore.QueryDocumentSnapshot;


import com.google.firebase.firestore.QuerySnapshot;



import java.sql.Timestamp;


import java.util.ArrayList;


import java.util.Collections;


import java.util.Date;


import java.util.List;



public class MainPageActivity extends AppCompatActivity implements View.OnClickListener {


 private TextView highestPulse, lowestPulse;


 private TextView highestTemp, lowestTemp;


 private TextView time;


 private ImageButton tempButton;


 private CardView pulseCardView, tempCardView;


 private Toolbar toolbar;


 private List<Integer> tempItemList;


 private List<Integer> pulseItemList;


 private List<WhtItem> itemList;


 private String htemp;


 private int minTemp ;


 private String ltemp;


 private int maxPulse;


 private String hpulse;


 private int minPulse;


 private String lpulse;


 private int maxTemp;


 private String dateAdded;



 private FirebaseAuth firebaseAuth;


 private FirebaseAuth.AuthStateListener authStateListener;


 private FirebaseUser user;


 private FirebaseFirestore db = FirebaseFirestore.getInstance();



 //Firestore conneection


 private CollectionReference collectionReference = db.collection("whtitem");



 @Override


 protected void onCreate(Bundle savedInstanceState) {


 super.onCreate(savedInstanceState);


 setContentView(R.layout.activity_main_page);



 toolbar = findViewById(R.id.toolbar);


 setSupportActionBar(toolbar);



 firebaseAuth = FirebaseAuth.getInstance();


 user = firebaseAuth.getCurrentUser();



 highestPulse = findViewById(R.id.highest_pulse_mainpage);


 lowestPulse = findViewById(R.id.lowest_pulse_mainpage);


 highestTemp = findViewById(R.id.highest_temperature_text);


 lowestTemp = findViewById(R.id.lowest_temp_text);


 time = findViewById(R.id.time_mainpage); //TARKISTA


 tempButton = findViewById(R.id.imageButton);


 pulseCardView = findViewById(R.id.pulse_cardView);


 tempCardView = findViewById(R.id.body_heat_cardview);


 tempItemList = new ArrayList<>();


 pulseItemList = new ArrayList<>();


 itemList = new ArrayList<>();



 tempButton.setOnClickListener(this);


 pulseCardView.setOnClickListener(this);


 tempCardView.setOnClickListener(this);


 }



 @Override


 public void onClick(View v) {


 switch (v.getId()){


 case R.id.imageButton:


 //TODO go to temp_history_activity


 break;


 case R.id.pulse_cardView:


 //TODO go to pulse_history_activity


 break;


 case R.id.body_heat_cardview:


 startActivity(new Intent(MainPageActivity.this, TimeTempActivity.class));


 break;


 }



 }



 @Override


 public boolean onCreateOptionsMenu(Menu menu) {


 getMenuInflater().inflate(R.menu.menu, menu);


 return super.onCreateOptionsMenu(menu);


 }



 @Override


 public boolean onOptionsItemSelected(MenuItem item) {


 switch (item.getItemId()){


 case R.id.profile:


 //add take users to add journal


 if (user != null && firebaseAuth!=null){


 startActivity(new Intent(MainPageActivity.this, ProfileActivity.class));


 //finish();


 }


 break;


 case R.id.temp_history:


 /*if (user != null && firebaseAuth!=null) {


 startActivity(new Intent(MainPageActivity.this, TempHistoryActivity.class));


 //finish();


 }


 break;*/


 case R.id.pulse_history:


 /*if (user != null && firebaseAuth!=null) {


 startActivity(new Intent(MainPageActivity.this, PulseHistoryActivity.class));


 //finish();


 }


 break;*/


 case R.id.time_in_temp_history:


 if (user != null && firebaseAuth!=null) {


 startActivity(new Intent(MainPageActivity.this, TimeTempActivity.class));


 //finish();


 }


 break;


 case R.id.action_signout:


 //signout


 if (user != null && firebaseAuth!=null){


 firebaseAuth.signOut();


 startActivity(new Intent(MainPageActivity.this, MainActivity.class));


 //finish();


 }


 break;


 }


 return super.onOptionsItemSelected(item);


 }



 @Override


 protected void onStart() {


 super.onStart();


 final Timestamp ts = new Timestamp(System.currentTimeMillis());


 final Date cDate = new Date(ts.getTime());


 final String currentDate = DateFormat.format("dd/MM/yyyy", cDate).toString();



 collectionReference.whereEqualTo("userId", WhtApi.getInstance()


 .getUserId())


 .whereEqualTo("date", cDate)


 .get()


 .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {


 @Override


 public void onSuccess(QuerySnapshot queryDocumentSnapshots) {


 if (!queryDocumentSnapshots.isEmpty()){



 for (QueryDocumentSnapshot whtitems : queryDocumentSnapshots) {


 WhtItem whtItem = whtitems.toObject(WhtItem.class);


 itemList.add(whtItem);



 Date dAdded = whtItem.getDate();


 dateAdded = DateFormat.format("dd/MM/yyyy", dAdded).toString();



 tempItemList.add(whtItem.getTemperature());


 pulseItemList.add(whtItem.getPulse());



 maxTemp = Collections.max(tempItemList);


 htemp ="Korkein:" + maxTemp +" astetta";


 minTemp = Collections.min(tempItemList);


 ltemp ="Matalin:" + minTemp +" astetta";


 maxPulse = Collections.max(pulseItemList);


 hpulse ="Korkein:" + maxPulse +" bpm";


 minPulse = Collections.min(pulseItemList);


 lpulse ="Matalin:" + minPulse +" bpm";


 }


 if (currentDate.equals(dateAdded)) {


 highestTemp.setText(htemp);


 lowestTemp.setText(ltemp);


 highestPulse.setText(hpulse);


 lowestPulse.setText(lpulse);


 }else {


 Toast.makeText(MainPageActivity.this,"Ei dataa tältä päivältä", Toast.LENGTH_SHORT).show();


 }


 }else {


 Toast.makeText(MainPageActivity.this,"Virhe dataa haettaessa", Toast.LENGTH_SHORT).show();


 }


 }


 })


 .addOnFailureListener(new OnFailureListener() {


 @Override


 public void onFailure(@NonNull Exception e) {


 Toast.makeText(MainPageActivity.this,"Ei dataa näytettäväksi", Toast.LENGTH_SHORT).show();



 }


 });



 }


}



这是我的POJO :


package com.example.wht.model;



import com.google.firebase.firestore.ServerTimestamp;



import java.util.Date;



public class WhtItem {


 private String nameUser, userId;


 private String password;


 private int temperature, pulse;


 private @ServerTimestamp java.util.Date date;


 private int duration;



 public WhtItem() {


 }



 public WhtItem(String nameUser, String userId, String password, int temperature, int pulse, java.util.Date date, int duration) {


 this.nameUser = nameUser;


 this.userId = userId;


 this.password = password;


 this.temperature = temperature;


 this.pulse = pulse;


 this.date = date;


 this.duration = duration;


 }



 public String getNameUser() {


 return nameUser;


 }



 public void setNameUser(String nameUser) {


 this.nameUser = nameUser;


 }



 public String getUserId() {


 return userId;


 }



 public void setUserId(String userId) {


 this.userId = userId;


 }



 public String getPassword() {


 return password;


 }



 public void setPassword(String password) {


 this.password = password;


 }



 public int getTemperature() {


 return temperature;


 }



 public void setTemperature(int temperature) {


 this.temperature = temperature;


 }



 public int getPulse() {


 return pulse;


 }



 public void setPulse(int pulse) {


 this.pulse = pulse;


 }



 public java.util.Date getDate() {


 return date;


 }



 public void setDate(Date dateAdded) {


 this.date = dateAdded;


 }



 public int getDuration() {


 return duration;


 }



 public void setDuration(int duration) {


 this.duration = duration;


 }


}



谁能知道我在哪里出错了?

时间:

对FirebaseStore的筛选文档数据的查询:


 Query query = FirebaseFirestore.getInstance().collection(TABLE_KEY_COMMENT)


 .whereEqualTo("disucssionId",discussionID)


 .orderBy("date", Query.Direction.DESCENDING);



尝试使用此查询并传递正确的参数

...