Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update a Fragment do not working

I am 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, the 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.

enter image description here

FilterListFragment code:

    @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 code:

@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);
    }
like image 402
Leomar de Souza Avatar asked May 21 '18 13:05

Leomar de Souza


1 Answers

You should communicate with second fragment through activity. When you do something in one fragment that should affect second one then you should call some method in your activity that will call update method in the second fragment. So for example activity:

public class MainActivity extends Activity(){
   private Fragment fragmentOne, fragmentTwo;

   public void updateFragmentTwo(){
       fragmentTwo.updateListView();
   }
}

fragment:

public class FirstFragment extends Fragment{


   public void updateFragmentTwo(){
       ((MyActivity)getActivity()).updateFragmentTwo();
   }
}
like image 184
matip Avatar answered Sep 29 '22 12:09

matip