如何在单击按钮时获取autocompleteTextView的值?

我需要一些帮助。我有以下代码:


string hozzaadnivalo;


public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)

        {


            View view = LayoutInflater.From(container.Context).Inflate(Resource.Layout.pager_item, container, false);

            container.AddView(view);


            Button hozzaadas = view.FindViewById<Button>(Resource.Id.hozzaad);


            var autoCompleteOptions = new string[] { "Sajt", "Tej", "Kecske", "Barátnő", "piros", "alma" };

            ArrayAdapter autoCompleteAdapter = new ArrayAdapter(container.Context, Android.Resource.Layout.SimpleDropDownItem1Line, autoCompleteOptions);

            AutoCompleteTextView mautoCompleteTextView = view.FindViewById<AutoCompleteTextView>(Resource.Id.autoCompleteTextView1);

            mautoCompleteTextView.Adapter = autoCompleteAdapter;


            hozzaadas.Click += hozaadasListViewhez;

            hozzaadnivalo = mautoCompleteTextView.Text;

        }

private void hozaadasListViewhez(object sender, EventArgs e)

        {

            adapter.Add(mautoCompleteTextView.Text);

            adapter.NotifyDataSetChanged();

        }

所以我想将autocompleteTextView的文本添加到我的listview适配器中,但是我不能在那儿做,因为它在那里不存在。所以我做了一个等于mautoCompleteTextView.Text的字符串,但是它将为空,因为它将在程序启动时在用户未执行任何操作时运行。所以我的问题是,当用户按下按钮时,我无法获得mautoCompleteTextView.Text。如果有人可以阻止,那将非常感谢。


DIEA
浏览 198回答 1
1回答

杨__羊羊

您mautoCompleteTextView在方法内部进行声明,因此仅在该方法本地可用。如果将声明移到方法之外,则mautoCompleteTextView整个类都可以使用该声明。// declare it hereAutoCompleteTextView mautoCompleteTextView;// then instantiate it within the methodpublic override Java.Lang.Object InstantiateItem(ViewGroup container, int position)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;... // code omitted&nbsp; &nbsp; &nbsp; &nbsp;mautoCompleteTextView = view.FindViewById<AutoCompleteTextView>(Resource.Id.autoCompleteTextView1);&nbsp; &nbsp; &nbsp; &nbsp;... // code omitted&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP