1

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);
    }
}
Neal Mc Beal
  • 245
  • 3
  • 16

1 Answers1

0

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) {
    }
});
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
  • Hey Nabin. I've edited my question. I tried your suggestion but it doesn't work for me. The CursorLoader is inside the Fragment and the TextView is inside the Activity. When i try your suggestion, I get a null pointer exception on the Loader Manager – Neal Mc Beal Oct 07 '17 at 09:56
  • Did you try `getActivity().getLoaderManager()` ? – Nabin Bhandari Oct 07 '17 at 09:57
  • I don't get the opportunity to write getActivity because the TextChangeListener is already in the Activity. – Neal Mc Beal Oct 07 '17 at 10:01
  • Oh, I understood the opposite. Why don't you save the reference of fragment in your activity and use fragment.getLoaderManager();? – Nabin Bhandari Oct 07 '17 at 10:02
  • Hey i created now a reference to the Fragment. My code is `DoingFragment doingFragment = new DoingFragment` and then `doingFragment.getLoaderManager().restartLoader(DoingFragment.TODO_LOADER, null, null);` but this gives me an error: IllegalStateException: Fragment DoingFragment{e0221fa} not attached to Activity. TODO Loader is the ID of the Fragment CursorLoader – Neal Mc Beal Oct 07 '17 at 10:09
  • do not create a new reference. use old reference. – Nabin Bhandari Oct 07 '17 at 10:10
  • What do you mean? I reference on my Fragment that i create on start up. `doingFragment = new DoingFragment(); getSupportFragmentManager().beginTransaction().replace(R.id.main_frame, doingFragment, TAG_TODO).commit(); ` – Neal Mc Beal Oct 07 '17 at 10:27
  • Hello Nabin. I solved the problem... It was a really stupid one. I wrote `initLoader(TODO_LOADER, null, null);` not `initLoader(TODO_LOADER, null, this);` – Neal Mc Beal Oct 07 '17 at 11:00
  • Good for you. :) – Nabin Bhandari Oct 07 '17 at 11:01