带ListView的MonoDroid AlertDialg

时间:2013-03-14 13:52:29

标签: android android-layout xamarin.android monotouch.dialog

我对MonoDroid / Android开发还很陌生,我想知道是否有人可以指点我的示例或解释如何使用自定义ListView创建AlertDialog?

我需要一个带图像的名字列表。我能够创建一个带有列表的AlertDialog,如下所示:

List<string> sPeople = new List<string>();
for (int ndx = 0; ndx < od.PeopleAtLoc.Count; ndx++)
{
    ObjectPeople d = od.PeopleAtLoc[ndx];
    sPeople.Add (d.Name + "\n" + d.Type);
}
string[] stuff = sPeople.ToArray();

new AlertDialog.Builder(this)
    .SetTitle("Choose a Person:")
        .SetItems(stuff, (sender, args) => 
                  { 
            _bInDetails = true;
            Intent intent = new Intent(this, typeof(FindAPersonDetailActivity));
                                intent.PutExtra("PERSON_ID", od.PeopleAtLoc[(int)args.Which].ID);
                                intent.PutExtra ("PERSON_CITY", od.PeopleAtLoc[(int)args.Which].City);
            StartActivity(intent);
        })
        .Show();

但我真的需要有一个与每个项目关联的图像,这就是为什么我希望我可以使用带有ListView的AlertDialog。

感谢您的帮助!!

1 个答案:

答案 0 :(得分:3)

您可以将自定义View添加到Dialog。因此,您可以制作自定义ListView Adapter,使用ListView创建视图,对其进行充气,将其添加到Dialog并将Adapter设置为您的var customView = LayoutInflater.Inflate (Resource.Layout.CustomDialogListView, null); var listView = (ListView) customView.FindViewById(Resource.Id.ListView); listView.Adapter = new MyListViewAdapter(stuff); var builder = new AlertDialog.Builder(this); builder.SetView(customView); builder.SetPositiveButton(Resource.String.dialog_ok, OkClicked); builder.SetNegativeButton(Resource.String.dialog_cancel, CancelClicked); return builder.Create(); 自己的。

{{1}}