C# 如何在警报对话框生成器setSingleChoiceItems中更新值和选定索引

C# 如何在警报对话框生成器setSingleChoiceItems中更新值和选定索引,c#,android,xamarin,xamarin.android,C#,Android,Xamarin,Xamarin.android,这是我正在使用的适配器,您可以看到我的视图从自定义布局中膨胀,这里的问题是,在单击/按下某个项目后,它不会处理到我的项目\u选择的方法,这就是为什么我添加了自定义onClick,但问题是单击后我不会关闭微调器。急需帮助 { public class CustomSpinnerAdapter : ArrayAdapter<String> { String type = String.Empty; List<string> order

这是我正在使用的适配器,您可以看到我的视图从自定义布局中膨胀,这里的问题是,在单击/按下某个项目后,它不会处理到我的项目\u选择的方法,这就是为什么我添加了自定义onClick,但问题是单击后我不会关闭微调器。急需帮助

{
    public class CustomSpinnerAdapter : ArrayAdapter<String> {

        String type = String.Empty;
        List<string> orderList = new List<string>();
        Context context;
        PageTaskSearchView pageTaskSearchView = new PageTaskSearchView();
        String WCName = String.Empty;
        Bundle utilBundle;
        View viewPageTask;


        /// <summary>
        /// CUstom Spinner Array Adapter constructor
        /// </summary>
        /// <returns></returns>
        public CustomSpinnerAdapter(Context context, int textViewResourceId, List<String> objects, String type, Bundle utilBundle, View view)
                : base(context, textViewResourceId, objects) {
            this.type = type;
            this.orderList = objects;
            this.context = context;
            this.utilBundle = utilBundle;
            this.viewPageTask = view;
        }

        /// <summary>
        /// returns the total array count minus one
        /// </summary>
        /// <returns>int</returns>
        public override int Count
        {
            get
            {
                return base.Count;
            }
        }

        public override View GetDropDownView(int position, View convertView, ViewGroup parent)
        {
            return getCustomView(position, convertView, parent);
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView, ViewGroup parent)
        {
            LayoutInflater inflater = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
            var view = convertView;



            if (position == 0)
            {
                if(Constants.ORDER_LIST == type) {

                    view = inflater.Inflate(Resource.Layout.CustomSpinnerView, null, true);
                    Switch filterOrder = (Switch) view.FindViewById(Resource.Id.switch_filterOrder);

                    int isFiltered = utilBundle.GetInt(Constants.ORDER_SWITCH_TRIGGER, 0);
                    filterOrder.Checked = Constants.ORDER_SWITCH_CLOSED == isFiltered;

                    filterOrder.CheckedChange += delegate (object sender, CompoundButton.CheckedChangeEventArgs e)
                    {
                        int filterStatus = e.IsChecked ? Constants.ORDER_SWITCH_CLOSED : 0;
                        utilBundle.PutInt(Constants.ORDER_SWITCH_TRIGGER, filterStatus);
                        String workCenter = utilBundle.GetString(Constants.WCName, Constants.EMPTY_STRING);
                        orderList = pageTaskSearchView.retrieveOrderlist(workCenter);

                        base.Clear();
                        base.AddAll(orderList);
                        base.NotifyDataSetChanged();

                    };

                }

                else
                {
                        LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(0, 0);
                        view = base.GetDropDownView(position, convertView, parent);
                        TextView tv = (TextView)view;
                        tv.SetHeight(0);
                        tv.LayoutParameters = layoutparams;
                        tv.Visibility = ViewStates.Gone;
                        view = tv;
                }
            }
            else
            {
                if (Constants.ORDER_LIST == type)
                {
                    view = inflater.Inflate(Resource.Layout.ListItemWithCheckbox, parent, false);
                    LinearLayout layout = (LinearLayout)view.FindViewById(Resource.Id.orderLayout);
                    TextView orderId = (TextView)view.FindViewById(Resource.Id.listItem);

                    RadioButton selected = view.FindViewById<RadioButton>(Resource.Id.checkedRadio);
                    selected.Checked = utilBundle.GetString(Constants.SELECTED_ORDER_ID, Constants.EMPTY_STRING).Equals(orderList[position]);
                    orderId.Text = orderList[position];

                    return view;

                    //view.Click += delegate (object sender, EventArgs e)
                    //{
                    //    PageTaskSearchViewModel pageTaskSearchViewModel = new PageTaskSearchViewModel(this.Context, this.viewPageTask, null, utilBundle);
                    //    pageTaskSearchViewModel.setOrderText(orderList[position]);
                    //};


                }
                else
                {
                    view = base.GetDropDownView(position, null, parent);

                }
            }

            parent.VerticalScrollBarEnabled = false;
            return view;

        }
    }
}


您的
CustomSpinerAdapter
可以包含一个
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; }
}
然后在
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();
}
效果如下:

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

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

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>
<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 >

layoutDrop.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>
<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 >


Hi Vince,欢迎来到SO。这是纯Xamarin,安卓系统,对吗?如果是,请删除Xamarin.Forms标记。可能会产生误导。哦,没错,曼希在更新模型数据后,需要
NotifyDataSetChanged()
进行视图更新。您好,先生。我使用的是SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);。我的问题是在这行列表之后。Clear();list.AddRange(newList);NotifyDataSetChanged();检查过的收音机仍在工作checked@VinceGeralddelaCerna你可以分享更多关于这个问题的解释。您的意思是想取消选中选中的选项?OrderIdAdapter.SetDropDownViewResource(Android.Resource.Layout.SimplesInnerDropDownItem);试着把这个加到你的代码里,先生,看看有什么不同,它有无线电检查display@VinceGeralddelaCerna你可以看看我的
CustomSpinnerAdapter
,有一个
GetDropDownView
方法,这是自定义下拉视图。因此,我的方法不需要使用
SetDropDownViewResource
,也不能调用它。@VinceGraldElectrna您可以在此处共享gif屏幕截图和
CustomSpinnerAdapter
code。我的
customSpinerAdapter
继承自
BaseAdapter
,我想看看你的。