[英]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.