2017-10-07 101 views
0

我想要在CustomListAdapter中设置的备注存在,直到用户删除它们。无论用户关闭应用程序,手机重新启动还是其他任何内容,他们添加的备注都需要保留在那里直到删除。我试图通过获取我的选项卡中的Sharepreferences并将它们设置在CustomListAdapter中,但它们不会保存:CustomListAdapater SharedPreferences在关闭应用程序时不保存状态

我添加了一个计数器,所以我可以在稍后检索该值以删除并调用方法在customerListAdapter中添加注释以设置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); 
     } 
} 

看不到的地方我已经错了,我该怎么设置它们,然后在的onClick的customListAdapter内删除它们?

编辑:

我已经内选项卡中添加此:

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); 
+0

我可能错过了它,因为在您的问题中有很多代码,但我无法看到您在何处检索您的sharedpreference值。 – Kunu

+0

是的,只是意识到,我在customerAdapter中创建了一个getNotes,并通过它传递了首选项。如果它不起作用,只会尝试更新。 – Sam

+0

@Kunu我已经添加了一个新的方法来检索Sharepreferences,但仍然不工作 – Sam

回答

0

管理在Tab类中执行此操作。在用户点击按钮时设置sharedpreferences。然后再次创建视图时检查它是否为空。然后获取所有首选项并将其存储到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(); 
      } 
     } 
0

ü添加此代码来保存ü要 SharedPreferences.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); 并使用它

相关问题