如何在“警报对话框”构建器setSingleChoiceItems中更新值和选定的索引

问题描述 投票:0回答:1

如何在“警报对话框”构建器setSingleChoiceItems中更新值和选定的索引

 CustomSpinnerAdapter arrayAdapter = new CustomSpinnerAdapter(context, Android.Resource.Layout.SimpleListItemSingleChoice, orders, Constants.ORDER_LIST);
            builder.SetSingleChoiceItems(arrayAdapter, -1, new EventHandler<DialogClickEventArgs>(delegate (object sender, DialogClickEventArgs e) {
                // Get reference to AlertDialog
                var d = (sender as Android.App.AlertDialog);
                // Do something with selected index
                spinnerSelected.Text = orders[e.Which];
                pageTaskSearchViewModel.TriggerProceedButtonClick();

                // Dismiss Dialog
                d.Dismiss();
            }));
c# android xamarin xamarin.android
1个回答
0
投票

您的CustomSpinnerAdapter可以包含Update方法来为适配器重新加载数据,如下所示:

public class CustomSpinnerAdapter : BaseAdapter 
{
    Context context;
    List<string> list;

    public CustomSpinnerAdapter(Context context, List<string> list)
    {
        this.context = context;
        this.list = new List<string>();
        this.list.AddRange(list) ;
    }

    public override Java.Lang.Object GetItem(int position)
    {
        return list[position];
    }

    public void Update(List<string> newList)
    {
        list.Clear();
        list.AddRange(newList);
        NotifyDataSetChanged();
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        //return base.GetDropDownView(position, convertView, parent);
        var view = convertView;
        CustomSpinnerAdapterViewHolder holder = null;

        if (view != null)
            holder = view.Tag as CustomSpinnerAdapterViewHolder;

        if (holder == null)
        {
            holder = new CustomSpinnerAdapterViewHolder();
            var inflater = context.GetSystemService(Context.LayoutInflaterService).JavaCast<LayoutInflater>();
            //replace with your item and your holder items
            //comment back in
            view = inflater.Inflate(Resource.Layout.Itemlayout, parent, false);
            holder.Title = view.FindViewById<TextView>(Resource.Id.textView1);
            view.Tag = holder;
            holder.Title.Text = GetItem(position).ToString();
        }
        //fill in your items


        return view;
    }

    public override View GetDropDownView(int position, View convertView, ViewGroup parent)
    {
        //return base.GetDropDownView(position, convertView, parent);
        var view = convertView;
        CustomSpinnerAdapterViewHolder holder = null;

        if (view != null)
            holder = view.Tag as CustomSpinnerAdapterViewHolder;

        if (holder == null)
        {
            holder = new CustomSpinnerAdapterViewHolder();
            var inflater = context.GetSystemService(Context.LayoutInflaterService).JavaCast<LayoutInflater>();
            //replace with your item and your holder items
            //comment back in
            view = inflater.Inflate(Resource.Layout.layoutDrop, parent, false);
            holder.Title = view.FindViewById<TextView>(Resource.Id.textView1);
            view.Tag = holder;
            holder.Title.Text = list[position];
        }
        //fill in your items


        return view;
    }

    //Fill in cound here, currently 0
    public override int Count
    {
        get
        {
            return list.Count;
        }
    }

}

class CustomSpinnerAdapterViewHolder : Java.Lang.Object
{
    //Your adapter views to re-use
    public TextView Title { get; set; }
}

活动中,为Spinner初始化CustomSpinnerAdapter

customAdapter = new CustomSpinnerAdapter(this, planetNames);
spinner.Adapter = customAdapter;
spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);

然后在ItemSelected方法中,在修改数据时,可以调用Update方法:

private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
    Spinner spinner = (Spinner)sender;
    string toast = string.Format("The planet is {0}", spinner.GetItemAtPosition(e.Position));
    Toast.MakeText(this, toast, ToastLength.Long).Show();

    if (isFirstShow)
    {
        isFirstShow = false;
        return;
    }

    Android.App.AlertDialog.Builder dialog = new Android.App.AlertDialog.Builder(this);
    Android.App.AlertDialog alert = dialog.Create();
    alert.SetTitle("Title");
    alert.SetMessage("Change data for spinner !");
    alert.SetButton("OK", (c, ev) =>
    {
        // Ok button click task  
        planetNames[e.Position] = "new data";
        customAdapter.Update(planetNames);
        Console.WriteLine("Modified!");
    });
    alert.Show();
}

效果如下:

enter image description here

===============================更新=============== ===================

Itemlayout.xml:微调框下拉视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="getView()"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minWidth="25px"
        android:minHeight="25px"
        android:id="@+id/textView1"/>

</LinearLayout>

layoutDrop.xml:微调框下拉视图

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="getDropDownView()"
        android:textSize="18sp"
        android:textColor="#ff0000" />

</RelativeLayout >
© www.soinside.com 2019 - 2024. All rights reserved.