2017-03-11 20 views
1

私はXamarin、Visual studio 2015でC#を使ってAndroidアプリを開発しています。私は、ユーザーがボタンをクリックしたときに文字列値を入力しなければならないボックスを持っています。この値をスピナーに追加し、次の開いた状態で保存して再ロードして、彼が必要とせずに入力した値を選択できるようにします。それを再入力してください。今まで私は問題はありませんが、私は考えましたが、私が苦労しているのは、ユーザーが値を入力してからボタンをクリックした後、別の値を入力し、最後の値だけを保存してAPPが再オープンされたときのスピナー。私が欲しいのは、ユーザーが入力した各値を保存してスピナーに表示する必要があります。次に、ユーザーが前に入力した値を削除したい場合は、ここで 複数のユーザー入力をスピナーに保存するにはどうすればよいですか?

は、私がこれまで行っているものです:

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; 

これらのコードを追加して保存し、ユーザー入力をスピナーに、私はアプリを再度開くときには、ユーザーが2つの値を入力した場合のみ、最後の1が保存されています私が欲しいのは、それぞれの価値が救われることです。スピナーに表示されます。 は

+0

最新の値をprefsに保存するだけです。複数の値を格納する場合は、それらを配列またはリストに格納し、それらをprefsに直列化する必要があります。 – Jason

+0

putStringSetを使用してSharedPrefenceに文字列のコレクションを格納し、次にgetStringSetを使用してスピンを設定します。 – tahsinRupam

答えて

0

はこれを試してみてください..事前にありがとう:

// 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; 
    } 

} 

ホープ、このことができます!

+0

これはうまくいきましたが、スピナーが(1,2,3)をデフォルトとして含むようにしたい場合、ユーザーが別の値を追加したいと思った場合、私がコードで示したように、var項目には "1,2 、3 "と私のスピナーのアダプターとして設定されました。また、コード内で_spinneradapter.Insert(user、0)と_spinneradapter.NotifyDataSetChanged()の意味を理解できませんでした。 –

+0

また、ユーザーが入力した値の1つを削除したい場合は、アイテムを削除するボタンが必要です。また、私は、ユーザーが2つの同じ値を入力するか、またはヌル値を入力することを望んでいません。 @apineda –

関連する問題