android - 更新片段不起作用
问题描述
我正在学习 Android,在尝试开发应用程序时遇到以下情况:
我有一个 Activity 有两个Fragments
:ReminderListFragment
和FilterListFragment
. 第一个片段有一个提醒列表,第二个片段有一个过滤器列表,其中包含每个过滤器中注册的项目的名称和数量。但是,当我排除一些提醒时,FilterListFragment 值不会更新。当我删除其中一个过滤器时也会发生同样的事情(在这种情况下,它会删除所选过滤器的所有记录),它不会更新提醒列表。
FilterListFragment
代码:
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getGroupId() == R.id.context_menu_category) {
// Used to verify it it is the right context_menu //Gets the item
// position and gets the category in that position:
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
Category category = ((CategoryFilter) lvFilters.getAdapter().getItem(info.position)).getCategory();
// Switch between the options in the context menu(Edit and Delete)
switch (item.getItemId()) {
case R.id.edit:
// Passes the current reminder to be edited via Intent and
// Invokes edit method
DialogFragment newFragment = EditCategoryDialogFragment.newInstance(category);
newFragment.show(getFragmentManager(), "" + R.string.dialog_editcategory_title);
updateListView();
return true;
case R.id.delete:
// Invokes delete method
try {
// Deletes from the bank;
Controller.instance(getActivity().getApplicationContext()).deleteReminderByCategory(category);
Controller.instance(getActivity().getApplicationContext()).deleteCategory(category);
updateListView();
return true;
} catch (DBException e) {
Log.e(TAG, e.getMessage());
}
updateListView();
return true;
default:
return super.onContextItemSelected(item);
}
}
return super.onContextItemSelected(item);
}
ReminderListFragment
代码:
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getGroupId() == R.id.context_menu_reminder) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
Reminder reminder = (Reminder) contextMenuAdapter.getItem(info.position);
switch (item.getItemId()) {
case R.id.edit:
Intent editIntent = editIntent(reminder);
editIntent.putExtra("id", reminder.getId());
editIntent.putExtra("text", reminder.getText());
editIntent.putExtra("details", reminder.getDetails());
startActivity(editIntent);
updateListView(null);
return true;
case R.id.delete:
try {
Controller.instance(getActivity().getApplicationContext()).deleteReminder(reminder);
} catch (DBException e) {
Log.e(TAG, e.getMessage());
}
updateListView(null);
return true;
default:
return super.onContextItemSelected(item);
}
}
return super.onContextItemSelected(item);
}
解决方案
您应该通过活动与第二个片段进行通信。当您在一个片段中执行应该影响第二个片段的操作时,您应该在活动中调用一些方法,该方法将调用第二个片段中的更新方法。因此,例如活动:
public class MainActivity extends Activity(){
private Fragment fragmentOne, fragmentTwo;
public void updateFragmentTwo(){
fragmentTwo.updateListView();
}
}
分段:
public class FirstFragment extends Fragment{
public void updateFragmentTwo(){
((MyActivity)getActivity()).updateFragmentTwo();
}
}
推荐阅读
- android - androidx.mediarouter 抱怨 MediaBrowserCompat
- mongodb - 尝试连接mongodb单节点副本集超时异常
- postgresql - 优化名称重复数据删除查询
- java - 任何允许 Pivot 中的撇号 SQL 列别名的安全问题?
- javascript - 将参数传递给事件处理程序
- wpf - 在非主 TabItem 内时,绑定在 DataGrid 列标题中不起作用
- python - /skye/ u'\u201c' 处的 KeyError
- javascript - React - 重用外部包文件中的组件
- javascript - jQuery没有在下拉表单中选择正确的选项
- javascript - 如果属性存在,则对对象数组进行排序