每次打开CustomDialogFragment时都会初始化ArrayList

时间:2019-05-29 05:43:21

标签: android android-dialogfragment

每次用户选中一个复选框时,我都使用CustomDialogFragment中的Arraylist来存储值。并在用户取消选中时删除值。问题是每次用户关闭对话框时,arraylist中的值都会被删除。我认为这是由于每次用户打开对话框时都会重新初始化数组。

我当时正在考虑将数组存储在共享首选项中,但不确定这样做是否正确。请建议我如何有效地做到这一点。

CarTypeDialogFragment.java

public class CarTypeDialogFragment extends DialogFragment {

public interface OnInputListener {
    void sendInput(String input);
}

public OnInputListener mOnInputListener;

CheckBox type1, type2, type4;
String SedanCheckBoxesVal, SUVCheckBoxesVal, HatchBackCheckBoxesVal;

ArrayList<String> myList = new ArrayList<String>();
View v;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
    if (getArguments() != null) {
        //totalCars = getArguments().getString("cars","");
    }
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    if (v != null) {
        if ((ViewGroup) v.getParent() != null)
            ((ViewGroup) v.getParent()).removeView(v);
        return v;
    }
    v = inflater.inflate(R.layout.fragment_restype_dialog, container, false);


    type1 = (CheckBox) v.findViewById(R.id.checkBox);
    type1.setChecked(getFromSP("cb1"));

    type2 = (CheckBox) v.findViewById(R.id.checkBox2);
    type2.setChecked(getFromSP("cb2"));

    type4 = (CheckBox) v.findViewById(R.id.checkBox4);
    type4.setChecked(getFromSP("cb4"));

    type1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (((CheckBox) view).isChecked()) {
                SedanCheckBoxesVal = "'Accord','Avlon','Challenger'";
                saveInSp("cb1", true);
                myList.add(SedanCheckBoxesVal);
                mOnInputListener.sendInput(TextUtils.join("", myList));
                saveInSp("cb1enable", false);

            } else {
                myList.remove(SedanCheckBoxesVal);
                mOnInputListener.sendInput(TextUtils.join("", myList));
                saveInSp("cb1", false);
            }
         }
    });


    type2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //is chkIos checked?

            if (((CheckBox) view).isChecked()) {
                SUVCheckBoxesVal = "'GMC','BMW'";
                saveInSp("cb2", true);
                myList.add(SUVCheckBoxesVal);
                mOnInputListener.sendInput(TextUtils.join("", myList));
                saveInSp("cb2enable", false);

            } else {
                myList.remove(SUVCheckBoxesVal);
                mOnInputListener.sendInput(TextUtils.join("", myList));
                saveInSp("cb2", false);
            }
        }
    });


    type4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //is chkIos checked?

            if (((CheckBox) view).isChecked()) {
                HatchBackCheckBoxesVal = "'Polo','Micra','MiniCooper'";
                saveInSp("cb4", true);
                myList.add(HatchBackCheckBoxesVal);
                mOnInputListener.sendInput(TextUtils.join("", myList));
                saveInSp("cb4enable", false);
                type4.setEnabled(false);

            } else {
                myList.remove(HatchBackCheckBoxesVal);
                mOnInputListener.sendInput(TextUtils.join("", myList));
                mOnInputListener.sendInput("");
                saveInSp("cb4", false);
            }
        }
    });
    return v;
}

private boolean getFromSP(String key) {
    SharedPreferences preferences = getContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
    return preferences.getBoolean(key, false);
}

private void saveInSp(String key, boolean value) {
    SharedPreferences preferences = getContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putBoolean(key, value);
    editor.commit();

}

@Override
public void onStart() {
    super.onStart();

    Window window = getDialog().getWindow();
    WindowManager.LayoutParams windowParams = window.getAttributes();
    windowParams.dimAmount = 0.00f;
    windowParams.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    window.setAttributes(windowParams);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try {
        mOnInputListener = (OnInputListener) getActivity();
    } catch (ClassCastException e) {
        Log.e(TAG, "onAttach: ClassCastException: " + e.getMessage());
    }
}

@Override
public void onDestroyView() {
    //workaround for this issue: https://code.google.com/p/android/issues/detail?id=17423 (unable to retain instance after configuration change)
    if (getDialog() != null && getRetainInstance())
        getDialog().setDismissMessage(null);
    super.onDestroyView();
}
}

2 个答案:

答案 0 :(得分:0)

考虑到您没有保存很多数据,可以将字符串列表保存到SharedPreferences

在这种情况下,可以将列表保存在SharedPreferences中。

答案 1 :(得分:0)

您是正确的,每当您关闭并重新打开对话框时,ArrayList都会重新初始化为空。要在重新打开时保留该状态,您将需要在对话框关闭之前将其保留并在对话框打开时加载它,就像您当前正在使用复选框的选中状态一样。为此,可以使用SharedPrefs,因为您的列表不大。

但是,据我所知,您正在根据已检查状态(已存储状态)将项目添加到列表中。因此,您应该能够使用它们来构造列表,而不必也存储列表。

例如:

if (getFromSP("cb1")) {
    myList.add("'Accord','Avlon','Challenger'")
}

,其他复选框类似。您可以在onCreateView之后getFromSP并将其用于setChecked