工具栏中的 TextView 更改时通知 CursorLoader

Notify CursorLoader while TextView in Toolbar changed

我的工具栏中有一个 TextView,此 TextView 显示选定的日期(例如,2017 年 10 月 7 日星期六)。这个工具栏在我的 MainActivity 中,在这个 MainActivity 中我有一个带有 ListView 的片段。此 ListView 使用 ContentResolver 链接到我的 SQLiteDatabase,并且此数据库中的值按日期过滤。

我有以下问题:我希望在我更改 MainActivity 工具栏中的 TextView 时,我的 Fragment 中的 CursorLoader 得到通知。

例如:我将日期 (TextView) 设置为 2017 年 10 月 6 日,ListView 显示了 10 月 6 日的所有条目。

我用 TextView 上的 TextWatcher 尝试过,但它不起作用(不知道我必须设置 TextChangeListener 有多厉害),我用 Cursor Loader 的 onContentChanged() 方法尝试过。

对我来说没有任何用处...也许你有想法。

编辑:

只是为了更好地理解。我的日期变量是 public static final,所以我可以访问 CursorLoader。我的 Fragment CursorLoader 中的选择如下:

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

日期变量通过以下方法更新:

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

setupToolbar 方法中的日期参数来自我的 DatePicker 对话框:

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);
    }
}

在 TextView 中使用 TextChangeListener 并在文本更改时重新启动加载程序,如下所示:

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) {
    }
});