0

This is the Firebase Structure I want to filter items according to the Date, If the user selects the specific date via Date picker, I want to filter items and want to show it into the Recyclerview. I tried this method it's working only for the current date. If I choose another date Recyclerview Doesn't change. What went wrong here?

structure

My Tried code

  dbbookingDetails=FirebaseDatabase.getInstance().getReference("Booking").child(currentuserID);

        recyclerView=findViewById(R.id.recyclerbill);
        recyclerView.setHasFixedSize(true);
        progressBar=findViewById(R.id.progreebarid);

        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        mlist=new ArrayList<>();

        btndate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                DialogFragment datepicker=new DatepickerFragment();
                datepicker.show(getSupportFragmentManager()," date picker");

                ////////////////-------------///////////////////////
                Calendar c=Calendar.getInstance();
                int year=c.get(Calendar.YEAR);
                int month=c.get(Calendar.MONTH);
                int day=c.get(Calendar.DAY_OF_MONTH);

                Calendar cal=Calendar.getInstance();
                cal.set(Calendar.YEAR,year);
                cal.set(Calendar.MONTH,month);
                cal.set(Calendar.DAY_OF_MONTH,day);

                final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");

                 final String  sdate = format.format(cal.getTime());

                tvdate.setText(sdate);

                dbbookingDetails.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        mlist.clear();
                        for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren())
                        {
                            BookingDetails bd=dataSnapshot1.getValue(BookingDetails.class);
                            if(bd.getBookingDate.equals(sdate))
                            {
                                mlist.add(bd);
                            }

                        }

                        madapter=new Adapter_booking_details(MainActivity.this,mlist);
                        recyclerView.setAdapter(madapter);

                        madapter.notifyDataSetChanged();
                        progressBar.setVisibility(View.INVISIBLE);

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

            }
        });
FGH
  • 2,900
  • 6
  • 26
  • 59
  • 1
    Why are you storing the date as a String and not as a [timestamp](https://stackoverflow.com/questions/43584244/how-to-save-the-current-date-time-when-i-add-new-value-to-firebase-realtime-data)? – Alex Mamo Apr 04 '20 at 12:55
  • I'm developing this app for the delivery service. so I want to store Date, and this is easy for me to display – FGH Apr 04 '20 at 15:24
  • Storing data in that format is not useful for ordering. – Alex Mamo Apr 04 '20 at 15:32
  • I do not use that data for ordering, it's for counting the total orders – FGH Apr 04 '20 at 17:44

1 Answers1

1

I think u just define adapter only one time. Then u need get instance of adapter and update data through a function update in adapter. Should not set adapter for recycler view anytime you fetch data from server. Additional, make sure the data you fetched is correctly. Hope this help!

Chuong Le Van
  • 274
  • 1
  • 8
  • pls just define adapter one time at onCreated() then make a function updateData in adapter. Using that function updateData to update your data fetched from server into recycler view. example ```madapter.updateData(mlist)``` that done. Function updateData in adapter. datas is temp list in adapter to store data passed from activity ```fun updateData(bookingDetails: list) { datas.clear() datas.addAll(bookingDetails) notifyDataSetChanged() }``` – Chuong Le Van Apr 04 '20 at 15:54