java - 当日期保存为字符串时,来自 Firebase 实时数据库的基于日期的查询
问题描述
如果日期保存为字符串,是否可以从 Firebase 数据库运行基于日期的查询?或者可以转换吗?我希望能够搜索特定日期范围内的记录,但日期作为字符串保存和检索。我在添加记录时使用 aDatePickerDiaglog
来设置日期。添加记录的方法如下:
private void addAnimal(){
String breed = editTextBreed.getText().toString().trim();
String gender = spinnerGender.getSelectedItem().toString();
String source = editTextSource.getText().toString().trim();
String DOB = mDisplayDate.getText().toString().trim();
String tag = editTextTagNumber.getText().toString().trim();
if(TextUtils.isEmpty(breed)){
Toast.makeText(this, "You should enter a breed", Toast.LENGTH_LONG).show();
}
else if(TextUtils.isEmpty(source)){
Toast.makeText(this, "You should enter a source", Toast.LENGTH_LONG).show();
}
else if (TextUtils.isEmpty(DOB)){
Toast.makeText(this, "You should enter a Date of Birth", Toast.LENGTH_LONG).show();
}
else if (TextUtils.isEmpty(tag)){
Toast.makeText(this, "You should enter a Tag Number", Toast.LENGTH_LONG).show();
}
else if (tag.length() < 6){
Toast.makeText(this, "Please enter valid tag number", Toast.LENGTH_LONG).show();
}
else{
String id = databaseAnimal.push().getKey();
Animal animal = new Animal(id, breed, gender, source, DOB, tag);
databaseAnimal.child(id).setValue(animal);
Toast.makeText(this, "Animal added", Toast.LENGTH_LONG).show();
} }
这是我用来检索所有记录的代码。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_retrieved);
listView = findViewById(R.id.list_item);
databaseReference = FirebaseDatabase.getInstance().getReference("animals");
animalList = new ArrayList<>();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String tempListValue =(listView.getItemAtPosition(position).toString());
//String tempListValue = animalList.get(position).toString();
//.get(position).toString();
TextView animalID = (TextView) parent.findViewById(R.id.animalID);
String animalIDText = (String) animalID.getText();
TextView animalGender = (TextView) parent.findViewById(R.id.gender);
String animalGenderText = (String) animalGender.getText();
TextView animalDOB = (TextView) parent.findViewById(R.id.DOB);
String animalDOBText = (String) animalDOB.getText();
TextView source = (TextView) parent.findViewById(R.id.source);
String sourceText = (String) source.getText();
TextView animalBreed = (TextView) parent.findViewById(R.id.breed);
String animalBreedText = (String) animalBreed.getText();
TextView animalTag = (TextView) parent.findViewById(R.id.tag);
String animalTagText = (String) animalTag.getText();
//Multiple Values
Intent intent = new Intent(DataRetrieved.this, ViewAnimalDetails.class);
Bundle extras = new Bundle();
extras.putString("EXTRA_ID",animalIDText);
extras.putString("EXTRA_DOB",animalDOBText);
extras.putString("EXTRA_GENDER",animalGenderText);
extras.putString("EXTRA_SOURCE",sourceText);
extras.putString("EXTRA_BREED", animalBreedText);
extras.putString("EXTRA_TAG", animalTagText);
intent.putExtras(extras);
startActivity(intent);
//intent.putExtra("ListViewClickedValue", tempListValue);
startActivity(intent);
}
}); //List view item click ends
}
@Override
protected void onStart(){
super.onStart();
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot animalSnapshot : dataSnapshot.getChildren()){
Animal animal = animalSnapshot.getValue(Animal.class);
animalList.add(animal);
}
AnimalInfoAdapter animalInfoAdapter = new AnimalInfoAdapter(DataRetrieved.this, animalList);
listView.setAdapter(animalInfoAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
如果需要任何其他信息,请告诉我,谢谢!
解决方案
如果日期保存为字符串,是否可以从 Firebase 数据库运行基于日期的查询?
简单的答案是肯定的,你可以根据字符串进行查询,但结果会出错。查询文字字符串时得到的结果与基于Date
对象查询时得到的结果完全不同。
或者可以转换吗?
是的,它可以,但结果将与上述相同。解决这个问题的最简单的解决方案是将日期属性从转换String
为Date
并根据它查询数据。
推荐阅读
- javascript - 数组数据在嵌套的异步箭头函数循环中丢失
- macos - 我可以在 docker 镜像中开发 react-native 应用吗
- c - 堆内存指针从结构内的指针字段中丢失
- java - 设置 DeferredResult setResult 方法后很晚才发回响应
- javascript - 遍历内部 DOM 元素并使用 Angular 2+ 替换内部 DOM
- php - PHP:如果条件比较很多
- javascript - (Symfony 4 表单类型)如何根据
- .net - 使用 Epplus 从 Excel 文件中读取数据并将其加载到 SQL Server
- macos - VSCode nvm 在集成终端启动时抱怨前缀选项
- javascript - 不要嵌套三元表达式 - 替代