项目不从共享偏好中删除

时间:2017-06-24 20:30:15

标签: java android arraylist android-sharedpreferences

我有一个我在共享偏好中保存的列表我想在用户长按时从列表视图中删除一个项目,所以在长按一下,当你确认删除它时,应用会提示你删除或不删除的问题。这有效但当我回到活动时,列表仍然包含已删除的项目 这是onCreateView的代码。 tinyDB是sharedprefrences.Thank you

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_tab_secondsemester, container, false);
    btnAddNew = (Button)rootView.findViewById(R.id.btnAddNewYearOneSecondSemester);
    listView = (ListView)rootView.findViewById(R.id.listViewSubjectsMyCoursesSecondSemester);
    tinyDB = new TinyDB(getContext());
    storedList = new ArrayList<>(tinyDB.getListString("storedList"));
    generalList = new ArrayList<>();
    spinnerValues = new ArrayList<>();




    getActivity().setTitle("Year One");

    setHasOptionsMenu(true);

    btnAddNew.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(getContext(),R.style.DialogeTheme);
            // Setting Dialog Title
            alertDialog.setTitle("Add new");

            // Setting Dialog Message
            String getUsername = tinyDB.getString("Username");
            alertDialog.setMessage("Hello "+ getUsername + ", please write the new subject");
            final EditText input = new EditText(getContext());
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            input.setLayoutParams(lp);
            input.setTextColor(Color.parseColor("#f06292"));
            alertDialog.setView(input);
            alertDialog.setPositiveButton("Add", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    String getSubjectInput = input.getText().toString();
                    storedList.add(getSubjectInput);
                    tinyDB.putListString("storedList", storedList);
                    arrayAdapter.notifyDataSetChanged();


                }
            });

            alertDialog.setNegativeButton("CANCEL",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // Write your code here to execute after dialog
                            dialog.cancel();
                        }
                    });
            // Showing Alert Message
            alertDialog.create();
            alertDialog.show();

        }
    });

    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(final AdapterView<?> adapterView, View view, final int i, long l) {
            new SweetAlertDialog(getContext(), SweetAlertDialog.WARNING_TYPE)
                    .setTitleText("Delete")
                    .setContentText("Are you sure you want to delete this course")
                    .setConfirmText("YES")
                    .setCancelText("NO")
                    .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            storedList.remove(i);
                            arrayAdapter.notifyDataSetChanged();
                            tinyDB.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {
                                @Override
                                public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
                                    arrayAdapter.notifyDataSetChanged();
                                }
                            });
                            sweetAlertDialog.dismiss();

                        }
                    })
                    .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            sweetAlertDialog.dismiss();
                        }
                    })
                    .show();
            return true;
        }
    });


    arrayAdapter = new CustomListAdapter(getContext(),storedList);
    listView.setAdapter(arrayAdapter);
    arrayAdapter.notifyDataSetChanged();




    return rootView;
}

1 个答案:

答案 0 :(得分:0)

用户删除时使用此功能

public void removeAt(int position) {
    mDataset.remove(position);
    notifyItemRemoved(position);
    notifyItemRangeChanged(position, mDataSet.size());
}

mDataset是用于保存适配器的所有数据的数组/列表