I'm learning Android and when trying to develop an application I came across the following situation:
I have an Activity that has two Fragments: ReminderListFragment
and FilterListFragment
. The first fragment has a list of Reminders and the second, a list of filters with the name and number of items registered in each filter. However, when I exclude some Reminder, FilterListFragment
values are not updated. The same thing happens when I delete one of the filters (in which case it deletes all records for the selected filter), it does not update the Reminders list.
CodeforFilterListFragment
:
@OverridepublicbooleanonContextItemSelected(MenuItemitem){if(item.getGroupId()==R.id.context_menu_category){//Usedtoverifyititistherightcontext_menu//Getstheitem//positionandgetsthecategoryinthatposition:AdapterView.AdapterContextMenuInfoinfo=(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();Categorycategory=((CategoryFilter)lvFilters.getAdapter().getItem(info.position)).getCategory();//Switchbetweentheoptionsinthecontextmenu(EditandDelete)switch(item.getItemId()){caseR.id.edit://PassesthecurrentremindertobeeditedviaIntentand//InvokeseditmethodDialogFragmentnewFragment=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);
}
Code for 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);
}