首页 > 解决方案 > 更新片段不起作用

问题描述

我正在学习 Android,在尝试开发应用程序时遇到以下情况:

我有一个 Activity 有两个Fragments:ReminderListFragmentFilterListFragment. 第一个片段有一个提醒列表,第二个片段有一个过滤器列表,其中包含每个过滤器中注册的项目的名称和数量。但是,当我排除一些提醒时,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);
    }

标签: androidandroid-layoutandroid-fragments

解决方案


您应该通过活动与第二个片段进行通信。当您在一个片段中执行应该影响第二个片段的操作时,您应该在活动中调用一些方法,该方法将调用第二个片段中的更新方法。因此,例如活动:

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();
   }
}

推荐阅读