private void initializeUserVariableAdapter() {
   Sprite currentSprite = ProjectManager.getInstance().getCurrentSprite();
   Project currentProject = ProjectManager.getInstance().getCurrentProject();
   UserVariablesContainer userVariableContainer = currentProject.getUserVariables();
   adapter = userVariableContainer.createUserVariableAdapter(getActivity(), currentSprite);
   setListAdapter(adapter);
   adapter.setOnCheckedChangeListener(this);
   adapter.setOnListItemClickListener(this);
 }
  @Override
  public void onCheckedChange() {
    if (!inContextMode) {
      return;
    }

    updateActionModeTitle();
    Utils.setSelectAllActionModeButtonVisibility(
        selectAllActionModeButton,
        adapter.getCount() > 0 && adapter.getAmountOfCheckedItems() != adapter.getCount());
  }
  private void updateActionModeTitle() {
    String title =
        adapter.getAmountOfCheckedItems()
            + " "
            + getActivity()
                .getResources()
                .getQuantityString(
                    R.plurals.formula_editor_variable_context_action_item_selected,
                    adapter.getAmountOfCheckedItems());

    contextActionMode.setTitle(title);
  }
 @Override
 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
   adapter.setSelectMode(ListView.CHOICE_MODE_MULTIPLE);
   adapter.notifyDataSetChanged();
   mode.setTitle(
       "0 "
           + getActivity()
               .getResources()
               .getQuantityString(
                   R.plurals.formula_editor_variable_context_action_item_selected, 0));
   getActivity().findViewById(R.id.bottom_bar).setVisibility(View.GONE);
   addSelectAllActionModeButton(mode, menu);
   return true;
 }
  @Override
  public void onStart() {

    this.registerForContextMenu(getListView());
    getListView().setItemsCanFocus(true);
    getListView().setLongClickable(true);
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    getListView()
        .setOnItemLongClickListener(
            new OnItemLongClickListener() {
              @Override
              public boolean onItemLongClick(
                  AdapterView<?> arg0, View view, int position, long id) {
                if (!inContextMode) {
                  deleteIndex = position;
                  getActivity().openContextMenu(getListView());
                  return true;
                }
                return false;
              }
            });

    setAddButtonListener(getActivity());

    adapter.notifyDataSetChanged();

    super.onStart();
  }
        @Override
        public void onDestroyActionMode(ActionMode mode) {
          UserVariablesContainer userVariablesContainer =
              ProjectManager.getInstance().getCurrentProject().getUserVariables();
          if (!adapter.isEmpty()) {
            for (UserVariable var : adapter.getCheckedUserVariables()) {
              userVariablesContainer.deleteUserVariableByName(var.getName());
            }
          }

          adapter.notifyDataSetChanged();
          getActivity().sendBroadcast(new Intent(ScriptActivity.ACTION_VARIABLE_DELETED));
          adapter.setSelectMode(ListView.CHOICE_MODE_NONE);
          contextActionMode = null;
          inContextMode = false;
          getActivity().findViewById(R.id.bottom_bar).setVisibility(View.VISIBLE);
        }
 @Override
 public void onListItemClick(int position) {
   Log.d("catroid", "onListItemClick");
   if (!inContextMode) {
     FormulaEditorFragment formulaEditor =
         (FormulaEditorFragment)
             getActivity()
                 .getSupportFragmentManager()
                 .findFragmentByTag(FormulaEditorFragment.FORMULA_EDITOR_FRAGMENT_TAG);
     if (formulaEditor != null) {
       formulaEditor.addUserVariableToActiveFormula(adapter.getItem(position).getName());
       formulaEditor.updateButtonViewOnKeyboard();
     }
     KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
     onKey(null, keyEvent.getKeyCode(), keyEvent);
   }
 }
 @Override
 public void onFinishNewVariableDialog(Spinner spinnerToUpdate, UserVariable userVariable) {
   adapter.notifyDataSetChanged();
 }