Wassim Dernayka
Wassim Dernayka

Reputation: 57

How can I save multiple user input to a spinner?

I am working with c# on Xamarin, Visual studio 2015 to develop android app. I have a box where user have to input a string value then when a button clicked, I want this value to be added to a spinner and to be saved and reloaded for the next open so he can choose the value he entered without the need to re-input it. Till now I don"t have a problem, I got my idea worked. But what I am struggling in is: if a user have input a value then clicked the button, then entered another value, only the last value is saved and showed in the spinner when the APP is re-opened. What I want is: each value entered by the user need to be saved and showed in the spinner. Then if the user want to delete the value he entered before, a button for delete item.

Here's what I have done so far:

string user;
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
var items = new List<string>() { "1", "2", "3" };
Button button4 = FindViewById<Button>(Resource.Id.button4);
Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner1);
EditText input = FindViewById<EditText>(Resource.Id.input);
user = input.Text;
button4.Click += delegate
        {
            user = input.Text;
            items.Add(user);
            ISharedPreferencesEditor editor = prefs.Edit();
            editor.PutString("try", user);
            editor.Apply();
        };
user = prefs.GetString("try", "no");
items.Add(user);
var adapter3 = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerItem, items);
spinner.Adapter = adapter3;

Those code are adding and saving the user input to the spinner when I reopen the app but if the user entered two values then only the last one is saved. What I want is each value to be saved and displayed in the spinner. Thank you in advance..

Upvotes: 1

Views: 138

Answers (1)

pinedax
pinedax

Reputation: 9346

Try this:

    // a constant to avoid magic strings
    const string KEY_FOR_TRY = "TRY";

    ArrayAdapter<string> _spinnerAdapter;

    protected override void OnCreate (Bundle savedInstanceState)
    {
        base.OnCreate (savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        string user;
        ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (this);

        // Load the items from pref and fill the spinner
        // if pref is empty just set an empty list.
        // GetStringSet because you are loading the collection you saved.
        var savedItems = prefs.GetStringSet (KEY_FOR_TRY, new List<string> ());

        var items = new List<string> (savedItems);

        Button button4 = FindViewById<Button> (Resource.Id.button4);
        Spinner spinner = FindViewById<Spinner> (Resource.Id.spinner1);
        EditText input = FindViewById<EditText> (Resource.Id.input);
        user = input.Text;

        button4.Click += delegate
                {
                    //you might want validate for empty strings and if entry is already saved to prevent duplicates.
                    user = input.Text;
                    items.Add (user);
                    ISharedPreferencesEditor editor = prefs.Edit ();

                    //PutStringSet because you are saving a collection.
                    editor.PutStringSet (KEY_FOR_TRY, items);
                    editor.Apply ();
                     //do this only if you want to refresh the spinner values.
                    _spinnerAdapter.Insert (user, 0);
                    _spinnerAdapter.NotifyDataSetChanged ();
                };

        //Get the first item if there is any, don't know why you need this.
        user = items.FirstOrDefault ();

        _spinnerAdapter = new ArrayAdapter<string> (this, Android.Resource.Layout.SimpleSpinnerItem, items);

        spinner.Adapter = _spinnerAdapter;
    }

}

Hope this helps!

Upvotes: 0

Related Questions