Problem with updating a Fragment

Asked

Viewed 133 times

1

I am learning Android and while trying to develop an app 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 quantity of items registered in each filter. However, when I delete any Reminder, the values of FilterListFragment are not updated. The same thing happens when I delete one of the filters (in this case it deletes all records referring to the selected filter), it does not update the list of Reminders.

inserir a descrição da imagem aqui

Code referring to 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);
        }

Code referring to 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);
    }
  • Can you show us the code for the updateListView() function? Looking at your code I am missing the info.notifyDataSetChanged() method; on your Adapter, probably putting it right after the delete actions will solve your problem, if you are not using this method within the updateListView() method then try to use it, if you don’t, post the update code that will be better for us to help.

  • @Eduardorafaelmoraes I tried to use info.notifyDataSetChanged() but he says that the method does not exist (I’m still beginner, I can’t tell you accurately about it, sorry)

1 answer

1


I was able to solve 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();
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.