0

enter image description here

    else if (item.getItemId() == R.id.sort_by_unprofitable_trades){
        Collections.sort(notesList, Model.UnProfitableTrades);
        adapter.notifyDataSetChanged();
    }
    else if(item.getItemId() == R.id.sort_by_date_range){
        materialDatePicker.show(getSupportFragmentManager(), "Tag_picker");
        materialDatePicker.addOnPositiveButtonClickListener(new 
        MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
            @Override
            public void onPositiveButtonClick(Pair<Long,Long> selection) {
                Long selctedstartDate = selection.first;
                Long selectedendDate = selection.second;
                startDateString = DateFormat.format("yyyy/MM/dd", new 
                Date(selctedstartDate)).toString();
                endDateString = DateFormat.format("yyyy/MM/dd", new 
                 Date(selectedendDate)).toString();
                //Collections.sort(notesList, Model.SortByDaterange);
                sortByDateRange();
                adapter.notifyDataSetChanged();

DataBase class:- void sortbyDateRange(String startDate, String endDate){ SQLiteDatabase database = this.getWritableDatabase(); String query = "SELECT * FROM " + TableName + " WHERE " + ColumnTradeDate + " BETWEEN " + startDate + " AND " + endDate ; database.execSQL(query); }

In Database Inspector the SQL query is giving correct results but no change is happening in the recycler view. What am I missing?

0

1 Answer 1

0

between query doesn't support any string value. Because in string value comparison not possible so you need to do the operation with float, Int and long type data .. I have faced the same problem then I converted my date to in long type data by using calender.timeInMillis .if u do like that you have to make a column in your table long type than send in to your database then do between query

pick your date and convert it to long type

   val datepickerdialog =
            DatePickerDialog(requireContext(), { view, year, monthOfYear, dayOfMonth ->

                
                srchDate2_id.text = "$dayOfMonth-$month-$year"

                calender2 = Calendar.getInstance()
                calender2.set(Calendar.DAY_OF_MONTH, dayOfMonth)
                calender2.set(Calendar.MONTH, monthOfYear)
                calender2.set(Calendar.YEAR, year)


                dt2 = calender2 .timeInMillis// send it to database you can do the between query with this value 
                Log.d(TAG, "first time: $dt2")
              

            }, y, m, d)

        datepickerdialog.show()
    }

still problem comment below.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Mahmud, I found out that we can use BETWEEN operator on the text, see examples w3schools.com/sql/sql_between.asp . Attaching Screenshot of the database inspector the query in the code above gives out correct results
Can't upload screenshot due to less reputation but this query works : SELECT * FROM mynotes WHERE tradeDate BETWEEN "2022/01/01" AND "2022/02/13" ,Here all dates are in string format and even the column tradeDate is of type string.
sql , mysql, oracle , sqlite all are similar but not same .. I have tried with date type data too but failed .. than I did it with long type data because in some app you will see months are show like 2022/Feb/13 or 13-02-2022 you will face again another problem because sqlite doesn't support those format tutorialspoint.com/sqlite/sqlite_date_time.htm .. I think with this raw system you wont face bugs.. as I have tried convert to millisecond will be best try.
if you still want to try with string then see those answers stackoverflow.com/questions/9649709/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.