Android 更改工具栏中的TextView时通知游标加载程序
我的工具栏中有一个文本视图,该文本视图显示选定的日期(例如2017年10月7日星期六)。此工具栏位于我的MainActivity中,在此MainActivity中有一个带有ListView的片段。此ListView通过ContentResolver链接到我的SQLiteDatabase,并且此数据库中的值按日期过滤 我有以下问题:我希望每当我更改MainActivity工具栏中的TextView时,片段中的游标加载程序都会得到通知 例如:我将日期(TextView)设置为6。2017年10月,ListView显示6中的所有条目。十月 我在TextView上用一个TextWatcher进行了尝试,但它不起作用(我不知道如何设置TextChangeListener),我用游标加载程序的onContentChanged()方法进行了尝试 没有什么适合我的。。。也许你有个主意 编辑: 只是为了更好的理解。我的日期变量是publicstaticfinal,因此我可以在CursorLoader中访问它。my Fragment CursorLoader中的选择如下所示:Android 更改工具栏中的TextView时通知游标加载程序,android,sqlite,android-textwatcher,Android,Sqlite,Android Textwatcher,我的工具栏中有一个文本视图,该文本视图显示选定的日期(例如2017年10月7日星期六)。此工具栏位于我的MainActivity中,在此MainActivity中有一个带有ListView的片段。此ListView通过ContentResolver链接到我的SQLiteDatabase,并且此数据库中的值按日期过滤 我有以下问题:我希望每当我更改MainActivity工具栏中的TextView时,片段中的游标加载程序都会得到通知 例如:我将日期(TextView)设置为6。2017年10月,L
String selection = DoingContract.COLUMN_DATE + "=?";
String[] selectionArgs = {MainActivity.mDate};
日期变量按以下方法更新:
public void setupToolbar(String date) {
mDate = date;
mToolbarTextView.setText(mDate);
}
setupToolbar方法中的日期参数来自my 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);
}
}
在文本视图中使用
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) {
}
});
在文本视图中使用
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) {
}
});
嗨,纳宾。我已经编辑了我的问题。我试过你的建议,但对我不起作用。CursorLoader位于片段内部,TextView位于活动内部。当我尝试你的建议时,我在加载程序管理器上得到一个空指针异常。如果你尝试
getActivity().getLoaderManager()
?我没有机会写getActivity,因为TextChangeListener已经在Activity中了。哦,我理解的正好相反。为什么不在活动中保存fragment的引用并使用fragment.getLoaderManager();?嘿,我现在创建了一个对片段的引用。我的代码是DoingFragment DoingFragment=new DoingFragment
,然后是DoingFragment.getLoaderManager().restartLoader(DoingFragment.TODO_LOADER,null,null)代码>但这给了我一个错误:IllegalStateException:Fragment DoingFragment{e0221fa}未附加到活动。TODO Loader是片段游标LoaderHey Nabin的ID。我已经编辑了我的问题。我试过你的建议,但对我不起作用。CursorLoader位于片段内部,TextView位于活动内部。当我尝试你的建议时,我在加载程序管理器上得到一个空指针异常。如果你尝试getActivity().getLoaderManager()
?我没有机会写getActivity,因为TextChangeListener已经在Activity中了。哦,我理解的正好相反。为什么不在活动中保存fragment的引用并使用fragment.getLoaderManager();?嘿,我现在创建了一个对片段的引用。我的代码是DoingFragment DoingFragment=new DoingFragment
,然后是DoingFragment.getLoaderManager().restartLoader(DoingFragment.TODO_LOADER,null,null)代码>但这给了我一个错误:IllegalStateException:Fragment DoingFragment{e0221fa}未附加到活动。TODO Loader是片段游标加载程序的ID