Home Save an array to sharedpreferences and show it in listview
Reply: 3

Save an array to sharedpreferences and show it in listview

Algirdas ┼Żarkaitis
1#
Algirdas ┼Żarkaitis Published in 2018-02-14 09:56:27Z

I need to save user input to shared preferences and also show it from shared preferences in a listview. How can I do this?

Right now my code adds to listview, but does not save it.

ListView listView;
Button AddWebsiteBtn;
EditText WebsiteName;
String[] ListOfWebsites = new String[]{};
SharedPreferences preferences;
SharedPreferences.Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add);
    listView = (ListView) findViewById(R.id.list_of_websites);
    AddWebsiteBtn = (Button) findViewById(R.id.btn_submit);
    WebsiteName = (EditText) findViewById(R.id.website_name);

    //using this part to add it to listview, maybe will need to remove it when shared preferences will work
    final List< String > ListElementsArrayList = new ArrayList< String >
            (Arrays.asList(ListOfWebsites));

    final ArrayAdapter < String > adapter = new ArrayAdapter< String >
            (AddActivity.this, android.R.layout.simple_list_item_1,
                    ListElementsArrayList);

    listView.setAdapter(adapter);

    AddWebsiteBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            ListElementsArrayList.add(WebsiteName.getText().toString());
            adapter.notifyDataSetChanged();
        }
    });
}
Abhishek kumar
2#
Abhishek kumar Reply to 2018-02-14 10:14:59Z

First you have to store all of your list item to ArrayList then Store ArrayList to SharedPreference

Variable declaration

SharedPreferences shared;
ArrayList<String> arrPackage;

Variable Initialization :

  shared = getSharedPreferences("App_settings", MODE_PRIVATE);
 // add values for your ArrayList any where...
 arrPackage = new ArrayList<>();

.Store value to sharedPreference :

 private void packagesharedPreferences() {
   SharedPreferences.Editor editor = shared.edit();
   Set<String> set = new HashSet<String>();
   set.addAll(arrPackage);
   editor.putStringSet("DATE_LIST", set);
   editor.apply();
   Log.d("storesharedPreferences",""+set);
   }

Retrive value of sharedPreference:

 private void retriveSharedValue() {
   Set<String> set = shared.getStringSet("DATE_LIST", null);
   arrPackage.addAll(set);
   Log.d("retrivesharedPreferences",""+set);
 }
DEADMC
3#
DEADMC Reply to 2018-02-14 10:09:07Z

You can use GSON for serializing your Array to JSON String so you can store it in SharedPreferences as usual String.

GSON examples you can find in that post

ddassa
4#
ddassa Reply to 2018-02-14 10:09:30Z

Convert your array of strings into a comma separate value (csv) string and save it. Use String.join(",", new String[]{"A", "B"}); in Java 1.8

When retrieving take is as a string and convert back to String array. use yourString.split(",")

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.325706 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO