0

I want to save and append data in shared preferences and display each data in listview , but it seems my code not works well. I'm new in android and I've already tried my best below , any help much appreciated

public class MainActivity extends AppCompatActivity {
public static final String PREFS_KEY = "notes";
ArrayList<CharSequence> list_items = new ArrayList<>();
ArrayAdapter arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    StringBuilder stringBuilder = new StringBuilder();
    saveNote();

    ListView list = (ListView)findViewById(R.id.listview);

    arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list_items);
    list.setAdapter(arrayAdapter);
    ArrayList<String> noteList = new ArrayList<>(getNotes());
    for(int i = 0; i < noteList.size(); i++)
        stringBuilder.append("\n" + noteList.get(i));
    list_items.add(stringBuilder.toString());
    arrayAdapter.notifyDataSetChanged();
}

public Set<String> getNotes() {
    SharedPreferences settings = getApplicationContext().getSharedPreferences("notes", Context.MODE_PRIVATE);
    // Get notes
    Set<String> notes = settings.getStringSet(PREFS_KEY, new HashSet<String>());
    return notes;
}
public void saveNote() {
    String text = "New messages";
    SharedPreferences settings = getApplicationContext().getSharedPreferences("notes", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = settings.edit();
    // Get existing notes
    Set<String> notes = getNotes();
    // Add new note to existing notes
    notes.add(text);
    // Store notes to SharedPreferences
    editor.putStringSet(PREFS_KEY, notes);
    editor.apply();
}

}

McLarvel
  • 31
  • 6
  • I wonder if saving your list in the preferences is the best way out. Have you thought about a local db. – Evans Chepsiror Jul 15 '20 at 07:12
  • "seems my code not works well" - please add more details about what is happening? any error in the logs? – Sairaj Sawant Jul 15 '20 at 07:16
  • @EvansChepsiror , I already think about that but for now the best way to do is to save data using sharepreferences please help – McLarvel Jul 15 '20 at 07:25
  • are u getting single item every time from preference? – darwin Jul 15 '20 at 07:25
  • @SairajSawant there's no error but it didn't append the data in sharepreferences – McLarvel Jul 15 '20 at 07:26
  • also u forgot to call adapter.notifyDatasetChanged() after the for loop – darwin Jul 15 '20 at 07:26
  • @darwin Yes , the idea code provided is comes from here https://stackoverflow.com/questions/44035216/android-add-string-to-shared-preferences – McLarvel Jul 15 '20 at 07:27
  • u are adding String text = "New messages"; every time, and set does not allow duplicates entries. try to add new text every time. eg: String text = "New messages" + System.currentTimeMillis(); for testing. – darwin Jul 15 '20 at 07:29
  • @darwin Yes, I mistake but aside from that how can I add and append data in sharepreferences can you help me pls – McLarvel Jul 15 '20 at 07:32
  • you can save list of items to preference as single string with comma separated entries using putString(). and while adding new item, u have get the existing string and append new string with comma – darwin Jul 15 '20 at 07:35

0 Answers0