Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从单击listview行的按钮获取对象_Listview_Android Listview_Xamarin.android_Listviewitem - Fatal编程技术网

如何从单击listview行的按钮获取对象

如何从单击listview行的按钮获取对象,listview,android-listview,xamarin.android,listviewitem,Listview,Android Listview,Xamarin.android,Listviewitem,当我在ListView上单击一行上的按钮时,我正在尝试存储/获取对象 因此,这是我单击ListView行的代码,该行按预期工作 //Listview ListView mainListView = FindViewById<ListView>(Resource.Id.lstViewMain); //Custom adapter (taking a list "allComponents")

当我在ListView上单击一行上的按钮时,我正在尝试存储/获取对象

因此,这是我单击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;
但是当我在ListView行上点击按钮时实现了相同的逻辑

            //Edit button
            ImageButton btnEditComponent = FindViewById<ImageButton>(Resource.Id.imgBtnEditComponent);
            btnEditComponent.Click += BtnEditComponent_Click;
我得到错误“System.NullReferenceException:'对象引用未设置为对象的实例”。”

我设法在自定义ListView适配器类中实现了编辑按钮,但我不知道如何访问MainListView\u ItemClick函数中的“AdapterView.ItemClickEventArgs”,以便将对象/组件存储在变量中,就像MainListView\u 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;
尝试2:

            //Set on click listener for the Edit component Button
            editComponent.SetOnClickListener(this);

非常感谢

在适配器的
GetView()
方法中尝试以下操作:

//Edit component button
editComponent = row.FindViewById<ImageButton>(Resource.Id.imgBtnEditComponent);
editComponent.Tag = position;
//Set on click listener for the Edit component Button
editComponent.SetOnClickListener(this);
        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();
        }
            //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;
         }

//Edit component button
editComponent = row.FindViewById<ImageButton>(Resource.Id.imgBtnEditComponent);
editComponent.Tag = position;
//Set on click listener for the Edit component Button
editComponent.SetOnClickListener(this);
public void OnClick(View v)
    {
      int position = (int)v.Tag;
      var component = allComponents[position];
    }