【问题标题】:How to get object from Button click on listview row如何从按钮单击列表视图行获取对象
【发布时间】:2020-01-19 21:03:15
【问题描述】:

当我单击 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;
        //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: 'Object reference not set to an instance of an object.'”

我设法在自定义 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 android-listview xamarin.android listviewitem


    【解决方案1】:

    在你的适配器的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);
    

    那么(如果 allComponents 是您的数据源)

    public void OnClick(View v)
        {
          int position = (int)v.Tag;
          var component = allComponents[position];
        }
    

    【讨论】:

      猜你喜欢
      • 2020-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多