如何直接将数据快照转换为对象?

时间:2020-06-17 18:58:30

标签: android firebase firebase-realtime-database google-cloud-firestore

firebase DocumentSnapshot中有一个将数据转换为类似对象的代码

protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);

            tvDateResult = (TextView) findViewById(R.id.tv_dateresult);
            res = findViewById(R.id.tv_dateresult1);
            ress = findViewById(R.id.tv_dateresult2);
            btDatePicker = (Button) findViewById(R.id.bt_datepicker);
            date = findViewById(R.id.bt_datepicker1);
            get = findViewById(R.id.getprice);

            date.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showDateDialog2();
                }

            });

            btDatePicker.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showDateDialog();
                }

            });




                }




            private void showDateDialog2() {

                Calendar newCalendar = Calendar.getInstance();


                datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                        Calendar newDate = Calendar.getInstance();
                        newDate.set(year, monthOfYear, dayOfMonth);

                        res.setText(dateFormatter.format(newDate.getTime()));
                    }

                },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

                datePickerDialog.show();

                if(tvDateResult !=null || res !=null) {
                    try {
                        String a = tvDateResult.getText().toString();
                        String b = res.getText().toString();

                        Date aa = dateFormatter.parse(a);
                        Date bb = dateFormatter.parse(b);

                        long diff = bb.getTime() - aa.getTime() / 24 * 60 * 60 * 1000;


                        ress.setText((int) TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));


                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
             }

        private void showDateDialog(){


            Calendar newCalendar = Calendar.getInstance();


            datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                    Calendar newDate = Calendar.getInstance();
                    newDate.set(year, monthOfYear, dayOfMonth);

                    tvDateResult.setText(dateFormatter.format(newDate.getTime()));
                }

            },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

            datePickerDialog.show();
}

是否存在使用DataSnapshot而不是Document Snaphot进行相同处理的代码? 我正在将Firebase RealtimeDatabase用于我的应用程序

1 个答案:

答案 0 :(得分:0)

Firestore的实时数据库中的DocumentSnapshot.toObject方法等效于DataSnapshot.getValue

所以:

Class object = dataSnapshot.getValue(Class.class);