jeudi 1 octobre 2015

Getting View in listView item in fragment

I have a listview in a fragment. that listview have a checkbox on each item. I want to set all checkboxes (from sharedpreferences) on fragment start and get all of them on "save" button clicked. save process works currently but load not works. the code of "save" button OnClickListener:

private View.OnClickListener saveButtonListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mListener != null) {
                shp = getActivity().getSharedPreferences(Constants.PREFERENCES, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = shp.edit();
                for (int i = 0; i < gridView.getCount(); i++) {
                    CheckBox checkBox = (CheckBox) gridView.getChildAt(i).findViewById(R.id.checkbox);
                    editor.putBoolean("" + i, checkBox.isChecked());
                }
                editor.apply();

                editText = (EditText) view.findViewById(R.id.editText);
                if (!editText.getText().toString().isEmpty()) {
                    Utils utils = new Utils(getActivity());
                    utils.setName(editText.getText().toString());
                }
                mListener.navigationFragment(new MenuFragment());
            }
        }

    };

on this line: CheckBox checkBox = (CheckBox) gridView.getChildAt(i).findViewById(R.id.checkbox); I got checkbox currently and uses ischecked() and iterate. I uses this code in load process like this:

shp = getActivity().getSharedPreferences(Constants.PREFERENCES, Context.MODE_PRIVATE);

for (int i = 0; i < gridView.getCount(); i++) {
    CheckBox checkBox = (CheckBox) gridView.getChildAt(i).findViewById(R.id.checkbox);
    checkBox.setChecked(shp.getBoolean("" + i, true));
}

this is like that but NullPointerExeption retrieved. I used last code in onViewCreated, onResume and onStart too. any ideas are welcomed.




Aucun commentaire:

Enregistrer un commentaire