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

Save an array to sharedpreferences and show it in listview

user1465
1#
user1465 Published in August 16, 2018, 6:14 am

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();
        }
    });
}
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO