将日期另存为字符串时,从Firebase实时数据库中基于日期的查询

时间:2019-01-31 14:16:38

标签: java android firebase firebase-realtime-database

如果日期保存为字符串,是否可以从Firebase数据库运行基于日期的查询?还是可以转换?我希望能够搜索特定日期范围内的记录,但是日期已保存并作为字符串检索。添加记录时,我使用DatePickerDiaglog设置日期。以下是添加记录的方法:

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) {

            }
        });
    }

让我知道是否需要其他信息,谢谢!

1 个答案:

答案 0 :(得分:0)

  

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

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

  

还是可以转换?

是的,但是结果将与上面相同。解决此问题的最简单方法是将 date 属性从String转换为Date并根据它查询数据。