簡體   English   中英

關閉應用程序時,CustomListAdapater SharedPreferences未保存狀態

[英]CustomListAdapater SharedPreferences Not Saving State when closing App

我希望在CustomListAdapter中設置的注釋一直存在,直到用戶將其刪除。 無論用戶是關閉應用程序,手機重啟還是其他操作,他們添加的筆記都必須保留在那里直到刪除。 我試圖通過在選項卡中獲取Sharepreferences並在CustomListAdapter中進行設置來做到這一點,但它們無法保存:

我添加了一個計數器,以便稍后可以檢索該值以刪除並調用customerListAdapter中的方法addnote來設置SharedPreferences。

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final View v = inflater.inflate(R.layout.tab3, container, false);

    final EditText notes = (EditText) v.findViewById(R.id.editText);
    listView=(ListView)v.findViewById(R.id.list);

    final int cross = R.drawable.cross;
    notesofrules = new ArrayList<String>(); //initial data list

    pref =   getContext().getSharedPreferences("MyPref", MODE_PRIVATE);
    editor = pref.edit();

    adapter = new CustomListAdapter(getActivity(), notesofrules, cross);

    listView=(ListView) v.findViewById(R.id.list);
    listView.setAdapter(adapter); //set the adapter once, only manipulate the data within

    Button button = (Button)v.findViewById(R.id.button3);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
                String newNote = notes.getText().toString();
                adapter.addNote(newNote, counter, editor); //add new note to the adapter list
                counter++;
                adapter.notifyDataSetChanged(); //very important to notify adapter and refresh the listview
                notes.setText("");

        }
    });
    return v;
}

CustomListAdapter:

public class CustomListAdapter extends ArrayAdapter<String> {

        private final Activity context;
        private ArrayList<String> notes = new ArrayList<>();
        private ImageView image;
        private int imageCross; //make this a list if you have multiple images and add similar to notes list

        public CustomListAdapter(Activity context, ArrayList<String> notes, int imageCross) {
            super(context, R.layout.item,notes);
            // TODO Auto-generated constructor stub
            this.context=context;
            this.notes = notes;
            this.imageCross = imageCross;
        }

        public View getView(final int position, View view, ViewGroup parent) {
            LayoutInflater inflater = context.getLayoutInflater();
            final View rowView = inflater.inflate(R.layout.item, null, false);

            final TextView ruleNotesSet = (TextView) rowView.findViewById(R.id.textView1);
            image = (ImageView) rowView.findViewById(R.id.icon);

            image.setImageResource(imageCross);
            image.setOnClickListener(new Button.OnClickListener(){
                @Override
                public void onClick(View v){
                    notes.remove(position);
                    notifyDataSetChanged();

                }
            });

            ruleNotesSet.setText(notes.get(position));
            return rowView;
        }

        public void addNote(String data, int position, SharedPreferences.Editor editor) {
            editor.putString(Integer.toString(position), data);
            editor.commit();
            notes.add(data);
        }
}

無法看到我哪里出了問題,如何設置它們,然后在customListAdapter的onClick中刪除它們?

編輯:

我在Tab中添加了此代碼:

adapter.getNotes(pref);
listView.setAdapter(adapter);

這是CustomListAdapter中的getNotes方法:

public void getNotes(SharedPreferences pref)
        {
            for(String note : notes) {
                pref.getString(note, note);
            }
        }

一旦關閉,仍無法恢復到原來的狀態。

我還編輯了addNote方法:

    editor.putString(data, data);

您添加此代碼以保存所需的數據SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME,MODE_PRIVATE).edit(); editor.putInt(“ position”,position); editor.apply(); 並獲取SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME,MODE_PRIVATE); Int position = prefs.getInt(“ positon”,null); 並使用它

可以在Tab類中做到這一點。 在用戶單擊按鈕時設置共享首選項。 然后在再次創建視圖時檢查它是否為null。 然后獲取所有首選項並將其存儲在Map中,並將其傳遞給onCreate中的適配器。 哎呀 希望有一天能對某人有所幫助。

String notesInStorage = pref.getString(Integer.toString(counter), newNote);

  if(notesInStorage != null)
        {
            Map<String,?> keys = pref.getAll();

            for(Map.Entry<String,?> entry : keys.entrySet()){

                notesofrules.add(entry.getValue().toString());
                adapter = new CustomListAdapter(getActivity(), notesofrules, cross);
                listView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM