Android DialogFragment中的ListView将无法保持最新

Android DialogFragment中的ListView将无法保持最新,android,listview,android-fragments,android-arrayadapter,back-stack,Android,Listview,Android Fragments,Android Arrayadapter,Back Stack,我有一个非常恼人的问题与对话片段和backbackback 我有一个主活动,它承载一个ActiveMenuFragment ActiveMenuFragment有一个按钮,用于启动对话框FragmentActionSelector(所有方法都从MainActivity强制接口方法运行): 此片段显示一个包含操作列表的ListView。现在一切都很好。甚至以前添加的新操作也会显示在这里。现在,如果用户想要定义一个新动作,他们会按下DialogFragment中的一个按钮,启动DialogFragm

我有一个非常恼人的问题与对话片段和backbackback

我有一个主活动,它承载一个ActiveMenuFragment

ActiveMenuFragment有一个按钮,用于启动对话框FragmentActionSelector(所有方法都从MainActivity强制接口方法运行):

此片段显示一个包含操作列表的ListView。现在一切都很好。甚至以前添加的新操作也会显示在这里。现在,如果用户想要定义一个新动作,他们会按下DialogFragment中的一个按钮,启动DialogFragmentDefineWaction:

/**
    * Opens the action selection dialog, so the user may pick an action to
    * begin.
    */
   @Override
   public void openActionSelector() {
      actionSelectorFragment = DialogFragmentActionSelector.newInstance();
      Bundle args = new Bundle();
      args.putSerializable("Profile", currentProfile);
      actionSelectorFragment.setArguments(args);
      FragmentTransaction ft = this.getFragmentManager().beginTransaction();
      ft.addToBackStack("Action Selector");
      actionSelectorFragment.show(ft, "Action Selector");
   }
在用户输入新操作的数据后,他们点击确认按钮,该按钮本应更新上一个片段(DialogFragmeConstrationSelector)中的ListView,但不会。每次我通过Backback返回时,它都会显示相同的8个动作

   /**
    * Takes the action defined in the DefineNewAction fragment and adds it to
    * the user dictionary.
    */
   @Override
   public void addNewActionToDictionary(Action toAdd) {
      currentProfile.getDictionary().addDefinition(toAdd);
      actionSelectorFragment.updateProfile(currentProfile);
   }
以下是完整的DialogFragmentActionSelector及其ArrayAdapter供参考:

public class DialogFragmentActionSelector extends DialogFragment implements
      OnClickListener {

   private EditText searchBar;
   private Button defineNewActionBtn;
   private ListView actionList;
   private UserProfile user;
   private ListViewInterface mInterface;
   private ActionsAdapter adapter;

   public interface ListViewInterface {

      void openDefineNewAction();



      void setActiveMenuActionBar(String action);
   }



   public void onAttach(Activity activity) {
      super.onAttach(activity);
      if (activity instanceof ListViewInterface) {
         mInterface = (ListViewInterface) activity;
      } else {
         throw new ClassCastException(activity.toString()
               + " must implement ListViewInterface");
      }
   }



   /**
    * Constructor for the fragment.
    * 
    * @return
    */
   public static DialogFragmentActionSelector newInstance() {
      DialogFragmentActionSelector dfm = new DialogFragmentActionSelector();
      return dfm;
   }



   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
      View toReturn = inflater.inflate(
            R.layout.fragment_action_selector_layout, container, false);
      user = (UserProfile) this.getArguments().get("Profile");
      searchBar = (EditText) toReturn.findViewById(R.id.action_search_bar);
      searchBar.addTextChangedListener(new TextWatcher() {

         @Override
         public void afterTextChanged(Editable arg0) {
         }



         @Override
         public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
               int arg3) {
         }



         @Override
         public void onTextChanged(CharSequence arg0, int arg1, int arg2,
               int arg3) {
            adapter.updateList();
            adapter.getFilter().filter(arg0);
         }
      });
      defineNewActionBtn = (Button) toReturn
            .findViewById(R.id.define_new_action);
      defineNewActionBtn.setOnClickListener(this);
      actionList = (ListView) toReturn.findViewById(R.id.actions_list);
      ArrayList<Action> allActions = user.getDictionary().getAllActions();
      adapter = new ActionsAdapter(this.getActivity(),
            android.R.layout.simple_list_item_1, allActions, inflater);
      actionList.setAdapter(adapter);
      actionList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

         @Override
         public void onItemClick(AdapterView<?> parent, View clickedView,
               int index, long id) {
            Action selectedAction = (Action) parent.getItemAtPosition(index);
            mInterface.setActiveMenuActionBar(selectedAction.getActionName());
            dismiss();
         }
      });
      return toReturn;
   }



   public void updateProfile(UserProfile updatedProfile) {
      user = updatedProfile;
      ArrayList<Action> allActions = user.getDictionary().getAllActions();
      adapter = new ActionsAdapter(this.getActivity(),
            android.R.layout.simple_list_item_1, allActions, getActivity()
                  .getLayoutInflater());
      actionList.setAdapter(adapter);
      actionList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

         @Override
         public void onItemClick(AdapterView<?> parent, View clickedView,
               int index, long id) {
            Action selectedAction = (Action) parent.getItemAtPosition(index);
            mInterface.setActiveMenuActionBar(selectedAction.getActionName());
            dismiss();
         }
      });
      // this.updateListView();
   }



   private void updateListView() {
      adapter.updateList();
   }

   // TODO FIX CONSTRUCTOR. UPON RETURNING, IT REVERTS TO DEFAULT 8 ACTIONS
   // RATHER THAN NEW ONES.
   private class ActionsAdapter extends ArrayAdapter<Action> implements
         Filterable {

      LayoutInflater inflater;
      ArrayList<Action> actionsList;



      public ActionsAdapter(Context context, int resId,
            ArrayList<Action> objects, LayoutInflater inflater) {
         super(context, resId, objects);
         this.actionsList = objects;
         this.inflater = inflater;
      }



      @Override
      public int getCount() {
         return actionsList.size();
      }



      @Override
      public Action getItem(int position) {
         return actionsList.get(position);
      }



      @Override
      public long getItemId(int position) {
         return this.getItem(position).hashCode();
      }



      public void updateList() {
         ArrayList<Action> allActions = user.getDictionary().getAllActions();
         actionsList = allActions;
         notifyDataSetInvalidated();
         // notifyDataSetChanged();
      }



      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
         ViewHolder holder = null;
         Action toWrap = actionsList.get(position);
         if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(
                  R.layout.listview_row_action_selector, parent, false);
            holder.actionName = (TextView) convertView
                  .findViewById(R.id.action_title);
            convertView.setTag(holder);
         } else
            holder = (ViewHolder) convertView.getTag();
         holder.actionName.setText(toWrap.getActionName());
         return convertView;
      }

      private class ViewHolder {

         TextView actionName;
      }



      /**
       * Filters the adapter's database.
       */
      @Override
      public Filter getFilter() {
         Filter filter = new Filter() {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
               FilterResults results = new FilterResults();
               ArrayList<Action> matchingActions = new ArrayList<Action>();
               for (int i = 0; i < actionsList.size(); i++) {
                  Action actionToCheck = actionsList.get(i);
                  ArrayList<String> actionTags = actionToCheck.getTags();
                  String searchQuery = constraint.toString();
                  boolean foundMatch = false;
                  int k = 0;
                  int tagSize = actionTags.size();
                  if (tagSize != 0) {
                     while (!foundMatch && k < tagSize) {
                        if (actionTags.get(k).contains(searchQuery)) {
                           matchingActions.add(actionToCheck);
                           foundMatch = true;
                        }
                        k++;
                     }
                  }
               }
               results.count = matchingActions.size();
               results.values = matchingActions;
               return results;
            }



            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint,
                  FilterResults results) {
               // actionsList.clear();
               actionsList = (ArrayList<Action>) results.values;
               adapter.notifyDataSetChanged();
            }
         };
         return filter;
      }
   }



   @Override
   public void onClick(View v) {
      switch (v.getId()) {
      case R.id.define_new_action:
         mInterface.openDefineNewAction();
         break;
      default:
         break;
      }
   }



   public void addNewActionToDictionary(Action toAdd) {
      user.getDictionary().addDefinition(toAdd);
      adapter.add(toAdd);
      adapter.updateList();
   }
}
公共类DialogFragmentActionSelector扩展DialogFragment实现
onclick侦听器{
私人编辑文本搜索栏;
专用按钮定义功能BTN;
私有列表视图操作列表;
私有用户配置文件用户;
私有ListViewInterface mInterface;
私有操作适配器;
公共接口ListViewInterface{
void openDefineNewAction();
void setActiveMenuActionBar(字符串操作);
}
公共事务主任(活动){
超级转速计(活动);
if(ListViewInterface的活动实例){
mInterface=(ListViewInterface)活动;
}否则{
抛出新的ClassCastException(activity.toString()
+“必须实现ListViewInterface”);
}
}
/**
*片段的构造函数。
* 
*@返回
*/
公共静态对话框FragmentActionSelector newInstance(){
DialogFragmentActionSelector dfm=新建DialogFragmentActionSelector();
返回dfm;
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
查看toReturn=充气机。充气(
R.layout.fragment\u action\u selector\u布局,容器,false);
user=(UserProfile)this.getArguments().get(“Profile”);
searchBar=(EditText)toReturn.findViewById(R.id.action\u search\u bar);
addTextChangedListener(新的TextWatcher(){
@凌驾
public void PostTextChanged(可编辑arg0){
}
@凌驾
更改前的公共void(字符序列arg0、int arg1、int arg2、,
int arg3){
}
@凌驾
public void onTextChanged(字符序列arg0、int arg1、int arg2、,
int arg3){
adapter.updateList();
adapter.getFilter().filter(arg0);
}
});
DefineWactionBTN=(按钮)返回
.findviewbyd(R.id.define\u new\u action);
defineNewActionBtn.setOnClickListener(此);
actionList=(ListView)toreReturn.findViewById(R.id.actions\u list);
ArrayList allActions=user.getDictionary().getAllActions();
adapter=new ActionsAdapter(this.getActivity(),
android.R.layout.simple_list_item_1,allActions,充气机);
actionList.setAdapter(适配器);
actionList.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(适配器查看父级,查看单击查看,
int索引,长id){
Action selectedAction=(Action)parent.getItemAtPosition(索引);
mInterface.setActiveMenuActionBar(selectedAction.getActionName());
解雇();
}
});
回归回归;
}
公共无效更新配置文件(UserProfile updatedProfile){
user=updatedProfile;
ArrayList allActions=user.getDictionary().getAllActions();
adapter=new ActionsAdapter(this.getActivity(),
android.R.layout.simple_list_item_1,allActions,getActivity()
.getLayoutInflater());
actionList.setAdapter(适配器);
actionList.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(适配器查看父级,查看单击查看,
int索引,长id){
Action selectedAction=(Action)parent.getItemAtPosition(索引);
mInterface.setActiveMenuActionBar(selectedAction.getActionName());
解雇();
}
});
//this.updateListView();
}
私有void updateListView(){
adapter.updateList();
}
//TODO修复构造函数。返回后,它将恢复为默认的8个操作
//而不是新的。
私有类ActionsAdapter扩展了ArrayAdapter实现
可过滤{
充气机;
ArrayList actionsList;
公共行动ADAPTER(上下文,int resId,
ArrayList对象,布局(充气机){
超级(上下文、剩余、对象);
this.actionsList=对象;
这个。充气机=充气机;
}
@凌驾
public int getCount(){
返回actionsList.size();
}
@凌驾
公共操作getItem(内部位置){
返回操作列表。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回此.getItem(position).hashCode();
}
公共void updateList(){
ArrayList allActions=user.getDictionary().getAllActions();
动作列表=所有动作;
notifyDataSetionValidated();
//notifyDataSetChanged();
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
ViewHolder=null;
Action toWrap=actionsList.get(位置);
if(convertView==null){
holder=新的ViewHolder();
convertView=充气机。充气(
R
public class DialogFragmentActionSelector extends DialogFragment implements
      OnClickListener {

   private EditText searchBar;
   private Button defineNewActionBtn;
   private ListView actionList;
   private UserProfile user;
   private ListViewInterface mInterface;
   private ActionsAdapter adapter;

   public interface ListViewInterface {

      void openDefineNewAction();



      void setActiveMenuActionBar(String action);
   }



   public void onAttach(Activity activity) {
      super.onAttach(activity);
      if (activity instanceof ListViewInterface) {
         mInterface = (ListViewInterface) activity;
      } else {
         throw new ClassCastException(activity.toString()
               + " must implement ListViewInterface");
      }
   }



   /**
    * Constructor for the fragment.
    * 
    * @return
    */
   public static DialogFragmentActionSelector newInstance() {
      DialogFragmentActionSelector dfm = new DialogFragmentActionSelector();
      return dfm;
   }



   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
      View toReturn = inflater.inflate(
            R.layout.fragment_action_selector_layout, container, false);
      user = (UserProfile) this.getArguments().get("Profile");
      searchBar = (EditText) toReturn.findViewById(R.id.action_search_bar);
      searchBar.addTextChangedListener(new TextWatcher() {

         @Override
         public void afterTextChanged(Editable arg0) {
         }



         @Override
         public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
               int arg3) {
         }



         @Override
         public void onTextChanged(CharSequence arg0, int arg1, int arg2,
               int arg3) {
            adapter.updateList();
            adapter.getFilter().filter(arg0);
         }
      });
      defineNewActionBtn = (Button) toReturn
            .findViewById(R.id.define_new_action);
      defineNewActionBtn.setOnClickListener(this);
      actionList = (ListView) toReturn.findViewById(R.id.actions_list);
      ArrayList<Action> allActions = user.getDictionary().getAllActions();
      adapter = new ActionsAdapter(this.getActivity(),
            android.R.layout.simple_list_item_1, allActions, inflater);
      actionList.setAdapter(adapter);
      actionList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

         @Override
         public void onItemClick(AdapterView<?> parent, View clickedView,
               int index, long id) {
            Action selectedAction = (Action) parent.getItemAtPosition(index);
            mInterface.setActiveMenuActionBar(selectedAction.getActionName());
            dismiss();
         }
      });
      return toReturn;
   }



   public void updateProfile(UserProfile updatedProfile) {
      user = updatedProfile;
      ArrayList<Action> allActions = user.getDictionary().getAllActions();
      adapter = new ActionsAdapter(this.getActivity(),
            android.R.layout.simple_list_item_1, allActions, getActivity()
                  .getLayoutInflater());
      actionList.setAdapter(adapter);
      actionList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

         @Override
         public void onItemClick(AdapterView<?> parent, View clickedView,
               int index, long id) {
            Action selectedAction = (Action) parent.getItemAtPosition(index);
            mInterface.setActiveMenuActionBar(selectedAction.getActionName());
            dismiss();
         }
      });
      // this.updateListView();
   }



   private void updateListView() {
      adapter.updateList();
   }

   // TODO FIX CONSTRUCTOR. UPON RETURNING, IT REVERTS TO DEFAULT 8 ACTIONS
   // RATHER THAN NEW ONES.
   private class ActionsAdapter extends ArrayAdapter<Action> implements
         Filterable {

      LayoutInflater inflater;
      ArrayList<Action> actionsList;



      public ActionsAdapter(Context context, int resId,
            ArrayList<Action> objects, LayoutInflater inflater) {
         super(context, resId, objects);
         this.actionsList = objects;
         this.inflater = inflater;
      }



      @Override
      public int getCount() {
         return actionsList.size();
      }



      @Override
      public Action getItem(int position) {
         return actionsList.get(position);
      }



      @Override
      public long getItemId(int position) {
         return this.getItem(position).hashCode();
      }



      public void updateList() {
         ArrayList<Action> allActions = user.getDictionary().getAllActions();
         actionsList = allActions;
         notifyDataSetInvalidated();
         // notifyDataSetChanged();
      }



      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
         ViewHolder holder = null;
         Action toWrap = actionsList.get(position);
         if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(
                  R.layout.listview_row_action_selector, parent, false);
            holder.actionName = (TextView) convertView
                  .findViewById(R.id.action_title);
            convertView.setTag(holder);
         } else
            holder = (ViewHolder) convertView.getTag();
         holder.actionName.setText(toWrap.getActionName());
         return convertView;
      }

      private class ViewHolder {

         TextView actionName;
      }



      /**
       * Filters the adapter's database.
       */
      @Override
      public Filter getFilter() {
         Filter filter = new Filter() {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
               FilterResults results = new FilterResults();
               ArrayList<Action> matchingActions = new ArrayList<Action>();
               for (int i = 0; i < actionsList.size(); i++) {
                  Action actionToCheck = actionsList.get(i);
                  ArrayList<String> actionTags = actionToCheck.getTags();
                  String searchQuery = constraint.toString();
                  boolean foundMatch = false;
                  int k = 0;
                  int tagSize = actionTags.size();
                  if (tagSize != 0) {
                     while (!foundMatch && k < tagSize) {
                        if (actionTags.get(k).contains(searchQuery)) {
                           matchingActions.add(actionToCheck);
                           foundMatch = true;
                        }
                        k++;
                     }
                  }
               }
               results.count = matchingActions.size();
               results.values = matchingActions;
               return results;
            }



            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint,
                  FilterResults results) {
               // actionsList.clear();
               actionsList = (ArrayList<Action>) results.values;
               adapter.notifyDataSetChanged();
            }
         };
         return filter;
      }
   }



   @Override
   public void onClick(View v) {
      switch (v.getId()) {
      case R.id.define_new_action:
         mInterface.openDefineNewAction();
         break;
      default:
         break;
      }
   }



   public void addNewActionToDictionary(Action toAdd) {
      user.getDictionary().addDefinition(toAdd);
      adapter.add(toAdd);
      adapter.updateList();
   }
}