DialogFragment中的ListView未显示

时间:2015-09-16 18:21:59

标签: android listview xamarin

我想在我的MainActivity上实施一个按钮,触发DialogFragment里面的ListView。当我点击MainActivity中的按钮时,DialogFragment没有显示。开发环境为Xamarin.Android。这是我的实施:

MainActivity

namespace DialogExample
{
    [Activity (Label = "DialogExample", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        private Button mClickBtn;
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            mClickBtn = FindViewById<Button> (Resource.Id.myButton);
            mClickBtn.Click += MClickBtn_Click;
        }

        void MClickBtn_Click (object sender, EventArgs e)
        {

            FragmentTransaction transaction = FragmentManager.BeginTransaction ();
            Dialog_Form dialogList = new Dialog_Form ();
            dialogList.Show (transaction, "Dialog fragment");
        }
    }
}

Diaolog_Form

namespace DialogExample
{
    public class Dialog_Form:DialogFragment
    {
        private ListView myListView;
        private List<string> myList=new List<string> ();
        public Dialog_Form ()
        {
        }

        public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView (inflater, container, savedInstanceState);

            myList.Add ("Jason");
            myList.Add ("Kenny");
            myList.Add ("Tedd");

            var view = inflater.Inflate (Resource.Layout.dialog_form, container, false);

            myListView = view.FindViewById<ListView> (Resource.Id.myListView);

            ArrayAdapter adapter = new ArrayAdapter (Activity, Android.Resource.Layout.SimpleListItem1, myList);
            myListView.Adapter = adapter;

            return view;
        }
    }
}

0 个答案:

没有答案