'I need to filter the listview using recyclerView by date(between start date and end date) stored in Room Database

i am storing data in Amount in Roomdatabase with current date.The app successfully storing the data and showing me list stored item in recylerview with the current date. Now i need o filter the result sum of total amount i added by date like i want to show list with start date and end date. i can get the sum of above using query but can not populate the list between dates.

i hope i described the question so that you can understand. here is my Code

public class MainActivity extends AppCompatActivity {


TextView cash,card,tAmount,tDate;
RecyclerView recyclerView;
RoomAdapter roomAdapter;
Button btn;


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



    btn =(Button)findViewById ( R.id.fbtn );

    recyclerView=(RecyclerView)findViewById ( R.id.myrecView );
    recyclerView.setLayoutManager ( new LinearLayoutManager (this  ) );

    cash=(TextView)findViewById ( R.id.cashTxt );
    card=(TextView)findViewById ( R.id.cardText );
    tAmount=(TextView)findViewById ( R.id.total_amount );





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


            MaterialDatePicker.Builder<Pair<Long, Long>> materialDateBuilder = MaterialDatePicker.Builder.dateRangePicker();
            materialDateBuilder.setTitleText ( "Select date Range" );
            final MaterialDatePicker materialDatePicker=materialDateBuilder.build();
            materialDatePicker.show ( getSupportFragmentManager (), "MATERIAL_DATE_PICKER");

            materialDatePicker.addOnPositiveButtonClickListener( new MaterialPickerOnPositiveButtonClickListener ()
            {

                        @SuppressLint("SetTextI18n")
                        @Override
                        public void onPositiveButtonClick(Object selection) {

                            Pair selectedDates = (Pair) materialDatePicker.getSelection();

                            final Pair<Date, Date> rangeDate = new Pair<>(new Date((Long) selectedDates.first), new Date((Long) selectedDates.second));
                            Date startDate=rangeDate.first;
                            Date endDate=rangeDate.second;
                            //SimpleDateFormat simpleFormat = new SimpleDateFormat ("dd MMM yyyy HH:mm");

                            long sDates= startDate.getTime ();
                            long eDates=endDate.getTime ();



                            AppDatabase db = Room.databaseBuilder(getApplicationContext (), AppDatabase.class, "myincome").allowMainThreadQueries ().build();
                            UserDao userDao = db.userDao();
                            userDao.newAllExpensesFromTo ( sDates,eDates );
                            String cIncome= String.valueOf ( userDao.newAllExpensesFromTo ( sDates,eDates ) );
                            tAmount.setText ( cIncome );

                          // ArrayList<Income> incomes= new ArrayList<> (userDao.newAllExpensesFromTo ( sDates,eDates ));


                            ArrayList incomes=new ArrayList (userDao.newAllExpensesFromTo ( sDates,eDates ));


                            roomAdapter= new RoomAdapter ( incomes );
                            recyclerView.setAdapter ( roomAdapter );

                            roomAdapter.notifyDataSetChanged ();


                          //  Log.d ("SELECTED DATE :","Converted Dates"+  simpleFormat.format(sDates) + " Second : " + simpleFormat.format(eDates));

                            Log.d ("SELECTED DATE :","Converted Dates"+userDao.newAllExpensesFromTo ( sDates,eDates )+incomes.size () );

                        }
                    });





        }
    } );

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener ( new View.OnClickListener () {
        @Override
        public void onClick(View view) {

            getRoomData();
            getRoomDataSum();
            getCashSum();
            BottomSheet bottomSheet=new BottomSheet ();
            bottomSheet.show ( getSupportFragmentManager () ,bottomSheet.getTag () );
        }
    } );

    getRoomData();
    getRoomDataSum();
    getCashSum();

}

This is the query that returns the sum of amount i have added

    @Query("SELECT SUM (Amount) FROM myIncome WHERE Date BETWEEN :startDate AND :endDate")
int  newAllExpensesFromTo(Long startDate,Long endDate);

and this is adapter

public class RoomAdapter extends RecyclerView.Adapter<RoomAdapter.myViewHolder>{

List<Income> incomes;
Context context ;
List<Integer> customList;

public RoomAdapter(List<Income> incomes) {
    this.incomes = incomes;

}


public RoomAdapter(ArrayList<Income> cincomes) {
    this.incomes = cincomes;

}

@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view= LayoutInflater.from ( parent.getContext () ).inflate ( R.layout.fragment_blank ,parent,false);
    return new myViewHolder ( view );
}

@Override
public void onBindViewHolder(@NonNull myViewHolder holder, int position)
{
    
  Date date = incomes.get ( position ).getDate ();
  DateFormat dateFormat = new SimpleDateFormat ("dd-MM-yyyy HH:mm");
  String stringDate = dateFormat.format ( date );
  
    holder.AmountType.setText ( incomes.get ( position ).getType ()  );
    holder.textViewAmount.setText ( String.valueOf (  incomes.get (  position).getAmount () ));
    holder.company.setText ( incomes.get ( position ).getCompany ());

    String cName= (String) holder.company.getText ();



    holder.dateText.setText (stringDate);

}

@Override
public int getItemCount() {
    return incomes.size ();
}

class myViewHolder extends RecyclerView.ViewHolder {
    TextView textViewAmount,AmountType,company,dateText;
    ImageView imageView;
    public myViewHolder(@NonNull View itemView) {
        super ( itemView );

        AmountType=itemView.findViewById ( R.id.AmountType );
        textViewAmount=itemView.findViewById ( R.id.txtViewAmount );
        company=itemView.findViewById ( R.id.Company );
        imageView=(ImageView)itemView.findViewById ( R.id.img1 );
        dateText=(TextView)itemView.findViewById ( R.id.dateText);
    }
}

}



Solution 1:[1]

hi so finally i have found the solution. perhaps no one answer to my question. the problem i found was in my query in the above query i was trying to get sum of the result but i wanted to populate the list also. but the above query will only show you the sum will not show you the list of the result.

so the right query is

@Query("SELECT * FROM myIncome WHERE Date BETWEEN :startDate AND :endDate")
List<Income>  newAll(Long startDate,Long endDate);

this will bring the list and show the listView of items between your filter dates.thanks

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Naveed