【发布时间】:2017-10-07 12:41:02
【问题描述】:
我希望我在 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;
}
自定义列表适配器:
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);
【问题讨论】:
-
我可能错过了,因为您的问题中有很多代码,但我看不到您在哪里检索 sharedpreference 值。
-
是的,我刚刚意识到,我正在 customerAdapter 中创建一个 getNotes 并通过它传递首选项。不行就更新,试试看。
-
@Kunu 我添加了一种新方法来检索 Sharepreferences 但仍然无法正常工作
标签: java android android-layout listview android-fragments