[英]How to filter data from multiple documents by date Firestore, Android,Java
Could someone help me with a query I'm trying?有人可以帮我解决我正在尝试的查询吗? I have put some test data to Firestore to test my app.我已经将一些测试数据放到 Firestore 来测试我的应用程序。 I want to query the data of the current users signed in and then get only the data that was saved to the database today.我想查询当前登录用户的数据,然后只获取今天保存到数据库的数据。 I want to get the highest and lowest temperature of the current day and also the same for a pulse.我想获得当天的最高和最低温度,脉搏也一样。 At the moment my code gets all of those, but when I try to compare the current date to the one in the database, so I can retrieve only the data from today, I only get the newest one.目前我的代码得到了所有这些,但是当我尝试将当前日期与数据库中的日期进行比较时,我只能检索今天的数据,我只得到最新的。 So example if I have to documents from to day, I only get the latest one.例如,如果我必须从今天开始记录文件,我只会得到最新的一份。 My code looks like this at the moment:我的代码现在看起来像这样:
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();
}
});
}
}
And here is my POJO:这是我的 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;
}
}
My Firestore documents are in the following form: Firestore database我的 Firestore 文档采用以下形式: Firestore 数据库
Can someone tell where am I going wrong with this?有人能告诉我这哪里出了问题吗? I tried to google and search but no luck.我试图谷歌和搜索,但没有运气。
the query for filter document data of FirebaseStore : FirebaseStore过滤文档数据的查询:
Query query = FirebaseFirestore.getInstance().collection(TABLE_KEY_COMMENT)
.whereEqualTo("disucssionId",discussionID)
.orderBy("date", Query.Direction.DESCENDING);
try to use like this query and pass correct param尝试像这个查询一样使用并传递正确的参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.