关闭应用程序时,CustomListAdapater SharedPreferences不保存状态

时间:2017-10-07 12:41:02

标签: java android android-layout listview android-fragments

我希望我在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);

2 个答案:

答案 0 :(得分:0)

你添加这段代码来保存你想要的数据 SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME,MODE_PRIVATE).edit();  editor.putInt(&#34; position&#34;,position);  editor.apply(); 得到 SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME,MODE_PRIVATE); Int position = prefs.getInt(&#34; positon&#34;,null); 并使用它

答案 1 :(得分:0)

管理在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();
            }
        }