首页 > 解决方案 > 当日期保存为字符串时,来自 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) {

            }
        });
    }

如果需要任何其他信息,请告诉我,谢谢!

标签: javaandroidfirebasefirebase-realtime-database

解决方案


如果日期保存为字符串,是否可以从 Firebase 数据库运行基于日期的查询?

简单的答案是肯定的,你可以根据字符串进行查询,但结果会出错。查询文字字符串时得到的结果与基于Date对象查询时得到的结果完全不同。

或者可以转换吗?

是的,它可以,但结果将与上述相同。解决这个问题的最简单的解决方案是将日期属性从转换StringDate并根据它查询数据。


推荐阅读