2017-10-07 95 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); 並使用它

相關問題