Problem with updating a Fragment

1

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);
    }
    
asked by anonymous 21.05.2018 / 15:51

1 answer

1

I was able to resolve this problem by adding the following methods:

In class FilterListFragment :

public void reloadReminderListFragment() {
        Fragment currentFragment = getActivity().getFragmentManager().findFragmentById(R.id.listReminders);
        if (currentFragment instanceof ReminderListFragment) {
            FragmentTransaction fragTransaction = (getActivity()).getFragmentManager().beginTransaction();
            fragTransaction.detach(currentFragment);
            fragTransaction.attach(currentFragment);
            fragTransaction.commit();
        }
    }

In class ReminderListFragment :

public void reloadFilterListFragment() {
        Fragment currentFragment = getActivity().getFragmentManager().findFragmentById(R.id.listCategories);
        if (currentFragment instanceof FilterListFragment) {
            FragmentTransaction fragTransaction = (getActivity()).getFragmentManager().beginTransaction();
            fragTransaction.detach(currentFragment);
            fragTransaction.attach(currentFragment);
            fragTransaction.commit();
        }
    }
    
24.05.2018 / 00:56