How can I display ListView in Xamarin.Android Aler

2019-08-08 16:05发布

问题:

I want display Listview inside the Android Alert Dialog.

Please help me any one

  void SetDialog(IList<OrderInfo> orders, string contactGuid){

        var dialogView = this.BindingInflate(Resource.Layout.guest_name_list, null);

        var dialog = new AlertDialog.Builder(this.Context);
        dialog.SetView(dialogView);
        dialog.SetNegativeButton("Cancel", (s, a) => { });
        AlertDialog alertDialog = dialog.Create();
        alertDialog.Show();

        dialogView.FindViewById<TextView>(Resource.Id.AlertTitle).SetText(PartyDetailsViewModel.EditAddOrderActionTitle, null);
        dialogView.FindViewById<TextView>(Resource.Id.AlertTitleMessage).SetText(PartyDetailsViewModel.EditAddOrderActionMessage, null);
        dialogView.FindViewById<TextView>(Resource.Id.AddOrderButton).SetText(PartyDetailsViewModel.AddOrderButtonTitle, null);
        dialogView.FindViewById<TextView>(Resource.Id.AddOrderButton).Click += delegate {
            this.ViewModel.SelectedOrder = (PartyDetailsViewModel.AddOrderButtonTitle, contactGuid);
            alertDialog.Dismiss();
        };
       dialogView.FindViewById<ListView>(Resource.Id.GuestNameList).SetAdapter();
    }

回答1:

Dialog/ListView Example:

var dialogView = LayoutInflater.Inflate(Resource.Layout.guest_name_list, null);
AlertDialog alertDialog;
using (var dialog = new AlertDialog.Builder(this))
{
    dialog.SetView(dialogView);
    dialog.SetNegativeButton("Cancel", (s, a) => { });
    alertDialog = dialog.Create();
}
var items = new string[] { "Stack", "Over", "Flow" };
var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, items);
dialogView.FindViewById<ListView>(Resource.Id.GuestNameList).Adapter = adapter;
alertDialog.Show();