Search code examples
androidsqliteandroid-textwatcher

Notify CursorLoader while TextView in Toolbar changed


I have a TextView in my Toolbar and this TextView display a selected Date (e.g. Saturday, 7. October 2017). This Toolbar is in my MainActivity and inside this MainActivity i have a Fragment with a ListView. This ListView is linked to my SQLiteDatabase with a ContentResolver and the values in this database are filtered by their date.

I have the following problem: I want that the CursorLoader in my Fragment gets notified whenever i change the TextView in the MainActivity Toolbar.

For example: I set the date (TextView) to the 6. October 2017 and the ListView displays me all entries from the 6. October.

I tried it with a TextWatcher on the TextView but it doesn't work (didn't know how excatly i have to set up the TextChangeListener) and i tried it with the onContentChanged() method of the Cursor Loader.

Nothing works for me... Maybe you got an idea.

Edit:

Just for the better understanding. My date variable is public static final so i get access in the CursorLoader. The selection in my Fragment CursorLoader is the following:

String selection = DoingContract.COLUMN_DATE + "=?";
String[] selectionArgs = {MainActivity.mDate};

The date variable is updated in the following method:

public void setupToolbar(String date) {
    mDate = date;
    mToolbarTextView.setText(mDate);
}

The date param in the setupToolbar method comes from my DatePicker Dialog:

public void onDateSet(DatePicker datePicker, int year, int month, int day) {
    Calendar newDate = Calendar.getInstance();
    newDate.set(year, month, day);
    String date = mDateFormat.format(newDate.getTime());
    if (main != null) {
        main.setupToolbar(date);
    } else {
        entry.setupDate(date);
    }
}

Solution

  • Use TextChangeListener in a TextView and restart loader on text change as below:

    textView.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //set the charSequense s to some variable that the cursor loader have access to.
            //todo
            // then restart the loader.
            getLoaderManager().restartLoader(/*args*/); 
        }
    
        @Override
        public void afterTextChanged(Editable s) {
        }
    });