如何在单击按钮上从微调器中删除所选项?

时间:2017-03-14 20:36:58

标签: c# android xamarin save spinner

我正在开发一个Android应用程序,我在Xamarin中使用c#中的那些代码,Visual stuio:

const string KEY_FOR_TRY = "TRY";

ArrayAdapter<string> _spinnerAdapter;
List<string> items;

protected override void OnCreate (Bundle savedInstanceState)
{
    base.OnCreate (savedInstanceState);
    SetContentView (Resource.Layout.Main);

    string user;
    ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (this);
        ISharedPreferencesEditor editor = prefs.Edit();
    var savedItems = prefs.GetStringSet (KEY_FOR_TRY, new List<string> ());
_spinnerAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerItem, items);

 spinner.Adapter = _spinnerAdapter;

    items = new List<string> (savedItems) {"1", "2", "3"};

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

    button4.Click += delegate
            {
                user = input.Text;
                items.Add (user);
                editor.PutStringSet (KEY_FOR_TRY, items);
                editor.Apply ();
                _spinnerAdapter.Insert (user, 0);
                _spinnerAdapter.NotifyDataSetChanged ();
            };
    user = items.FirstOrDefault ();

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

    spinner.Adapter = _spinnerAdapter;
}

Button button5 = FindViewById<Button>(Resource.Id.button5);
        button5.Click += delegate
        {
            items.Remove(spinner.SelectedItem.ToString());
            editor.Remove(spinner.SelectedItem.ToString());
            editor.Apply();
            _spinnerAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerItem, items);
            spinner.Adapter = _spinnerAdapter;
        };
        user = items.FirstOrDefault();
        spinner.Adapter = _spinnerAdapter;

我想要button5spinner以及SharedPreference prefs删除用户选择的项目。另外,当我运行这些代码时,var items中的每个项目在微调器中显示两次,我该如何阻止它?并且(button4)如果用户输入了两次相同的值,我想显示一个toast message,表示您已经输入了这个值。

编辑: button5从微调器移除项目并且不会永久删除它,因此当我关闭并重新打开应用程序时,删除的项目会重新显示在微调器中。此外,var items中的每个项目都会在spinner中显示两次,但新添加的项目会显示一次。 请帮我解决这些问题.. 提前谢谢..

1 个答案:

答案 0 :(得分:0)

在OnCreate方法之外声明您的项目列表。

List<string> items;


protected override void OnCreate (Bundle savedInstanceState)
{
    base.OnCreate (savedInstanceState);
    SetContentView (Resource.Layout.Main);
    ....

    items = new List<string> (savedItems) {"1", "2", "3"};
    _spinnerAdapter = new ArrayAdapter<string> (this, Android.Resource.Layout.SimpleSpinnerItem, items);
    spinner.Adapter = _spinnerAdapter;    

    button5.Click += delegate {
         // To remove the selected item, first remove the selected item from the list, then call NotifyDataSetChanged
         items.remove(spinner.SelectedItem.ToString ());
         _spinnerAdapter.NotifyDataSetChanged ();
    };

}

并且至于项目在微调器中显示两次

var savedItems = prefs.GetStringSet (KEY_FOR_TRY, new List<string> ());
var items = new List<string> (savedItems) {"1", "2", "3"};

你这样做是对的吗?您的列表现在包含集合savedItems + {&#34; 1&#34;,&#34; 2&#34;,&#34; 3&#34;}

编辑:

抱歉,我错了。使用微调器&#39; NotifyDataSetChanged()&#39;好像没有用。您需要再次设置适配器。

public class MainActivity : Activity
{
    Spinner mySpinner;
    Button button;

    List<string> items;
    ArrayAdapter<string> _spinnerAdapter;

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

        SetContentView (Resource.Layout.Main);

        button = FindViewById<Button> (Resource.Id.myButton);
        mySpinner = FindViewById<Spinner> (Resource.Id.mySpinner);

        items = new List<string> { "1", "2", "3" };

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

        button.Click += delegate {
            items.Remove (mySpinner.SelectedItem.ToString ());
            mySpinner.Adapter = new ArrayAdapter<string> (this, Android.Resource.Layout.SimpleSpinnerItem, items);
        };
    }
}

这很有效。我已经过测试了。

编辑:

替换

 editor.Remove(spinner.SelectedItem.ToString());

使用:

 editor.PutStringSet (KEY_FOR_TRY, items);
 editor.Apply ();

您尝试从SharedPreference中删除微调器中的所选项目,而所选项目位于SharedPreference内的列表中。