如何从按钮上单击列表视图行上的对象

问题描述 投票:0回答:1

当我单击ListView的一行上的Button时,我试图存储/获取对象。

因此,这是我的代码,用于单击按预期工作的ListView行

            //Listview
            ListView mainListView = FindViewById<ListView>(Resource.Id.lstViewMain);

            //Custom adapter (taking a list "allComponents")
            ComponentListViewAdapter adapter = new ComponentListViewAdapter(this, allComponents);
            mainListView.Adapter = adapter;

            //Listview item/row click
            mainListView.ItemClick += MainListView_ItemClick;
        //Function to show the component's notes
        private void MainListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {
            //Get the component tapped
            var component = allComponents[(int)e.Id];

            //Display message that the component was tapped
            Toast.MakeText(this, component.Name.ToString() + " Tapped", ToastLength.Long).Show();

            // Declare the activity as intent
            var intent = new Intent(this, typeof(View_NotesActivity));

            //Transfer the component's notes to the notes screen for display
            intent.PutExtra("component_notes", component.Notes);

            //Start the activity of intent
            StartActivity(intent);
         }

尽管当我实现与单击ListView行上的按钮时相同的逻辑时>

            //Edit button
            ImageButton btnEditComponent = FindViewById<ImageButton>(Resource.Id.imgBtnEditComponent);
            btnEditComponent.Click += BtnEditComponent_Click;
        private void BtnEditComponent_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

我收到错误“ System.NullReferenceException:'对象引用未设置为对象的实例。'”

我设法在自定义ListView适配器类中实现了编辑按钮,但我不知道如何像在MainListView_ItemClick函数中那样访问“ AdapterView.ItemClickEventArgs e”,以便将对象/组件存储在变量中,就像在MainListView_ItemClick函数如下:

        //Function to show the component's notes
        private void MainListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {
            //Get the component tapped
            var component = allComponents[(int)e.Id];

这是在自定义ListView适配器类中实现“编辑”按钮(ListView上的按钮)的两次尝试,以其他方式,我知道如何访问/存储给定行的“编辑”按钮所在的组件。

            //Edit component button
            editComponent = row.FindViewById<ImageButton>(Resource.Id.imgBtnEditComponent);
            editComponent.Click += EditComponent_Click;

尝试1:

        private void EditComponent_Click(object sender, EventArgs e)
        {
            //Get the component tapped
            //This does not work like it does with the ListView row tap function.
            //var component = allComponents[(int)e.Id];

            Toast.MakeText(mContext, e.ToString() + " Edited", ToastLength.Long).Show();
            //Retuns "System.EventArgs Edited"

            //throw new NotImplementedException();
        }

尝试2:

            //Set on click listener for the Edit component Button
            editComponent.SetOnClickListener(this);
        public void OnClick(View v)
        {
            switch (v.Id)
            {
                case Resource.Id.imgBtnEditComponent:

                 //How can I grab the component when the only argument is a View?

                 break;
         }

非常感谢

当我单击ListView上一行的Button时,我试图存储/获取对象。因此,这是我的代码,用于单击按预期工作的ListView行// Listview ...

listview android-listview xamarin.android listviewitem
1个回答
0
投票

尝试使用适配器的GetView()方法:

© www.soinside.com 2019 - 2024. All rights reserved.