【问题标题】:Xamarin.android - get information from a ListViewXamarin.android - 从 ListView 获取信息
【发布时间】:2017-11-02 21:50:07
【问题描述】:

我是 Xamarin 的新手,我创建了一个视图,其中显示了项目列表,我创建了活动、axml 文件和样式。它运作良好。

现在我想对选定的项目启动一个“意图”,但我无法获取项目的位置或标题。

我的“Console.WriteLine(selectedFromList)”仅显示 0 值,我想从我选择的项目中获取更多信息,例如正确的位置或标题,以便我可以验证和“意图”我选择的每个项目的具体活动。

namespace app_sofis.Droid
{
    [Activity(Label = "ServiziActivity" , Theme = "@style/ThemeActionBar")]
    public class ServiziActivity : Activity
    {

        public static int TYPE_ITEM = 0;
        public static int TYPE_SEPORATOR = 1;

        List<IMenuItemsType> item = new List<IMenuItemsType>();

        private ListView lst;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

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

            // my items
            item.Add(new MenuHeaderItem("Servizi per il paziente"));
            item.Add(new MenuContentItem("COLLO", "Inforntuni, patologie, interventi e riabilitazione", Resource.Mipmap.ic_schiena));
            item.Add(new MenuContentItem("SPALLA", "Inforntuni, patologie, interventi e riabilitazione", Resource.Mipmap.ic_schiena));
            item.Add(new MenuContentItem("SCHIENA", "Inforntuni, patologie, interventi e riabilitazione", Resource.Mipmap.ic_schiena));


            lst = FindViewById<ListView>(Resource.Id.lstview);
            lst.Adapter = new ListViewAdapter(this, item);

            lst.ItemClick += (object sender, Android.Widget.AdapterView.ItemClickEventArgs e) =>
            {
                // HERE I WOULD LIKE VALIDATE ITEM AND START "intent" with my specific activities :)
                string selectedFromList = lst.GetItemAtPosition(e.Position).ToString();
                // trying to show item position but it return always 0
                Console.WriteLine(selectedFromList);
            };

        }

        // code for my listview

        public interface IMenuItemsType
        {
            int GetMenuItemsType();
        }

        public class MenuHeaderItem : IMenuItemsType
        {
            public string HeaderText { get; set; }

            public int GetMenuItemsType()
            {
                return TYPE_ITEM;
            }

            public MenuHeaderItem(string _headerText)
            {
                HeaderText = _headerText;
            }
        }

        public class MenuContentItem : IMenuItemsType
        {
            public string Title { get; set; }
            public string SubTitle { get; set; }
            public int IconImage { get; set; }

            public int GetMenuItemsType()
            {
                return TYPE_SEPORATOR;
            }

            public MenuContentItem(string _title, string _subtitle, int _iconImage)
            {
                Title = _title;
                SubTitle = _subtitle;
                IconImage = _iconImage;
            }
        }



        public class ListViewAdapter : ArrayAdapter<IMenuItemsType>
        {
            private Context context;
            private List<IMenuItemsType> items;
            private LayoutInflater inflater;

            public ListViewAdapter(Context context, List<IMenuItemsType> items) : base(context, 0, items)
            {
                this.context = context;
                this.items = items;
                this.inflater = (LayoutInflater)this.context.GetSystemService(Context.LayoutInflaterService);
            }


            public override int Count
            {
                get
                {
                    //throw new System.NotImplementedException();
                    return items.Count;
                }
            }

            public override long GetItemId(int position)
            {
                //throw new System.NotImplementedException();
                return position;
            }

            public override View GetView(int position, View convertView, ViewGroup parent)
            {
                //throw new System.NotImplementedException();
                View view = convertView;
                try
                {
                    IMenuItemsType item = items[position];
                    if (item.GetMenuItemsType() == TYPE_ITEM)
                    {
                        MenuHeaderItem _headerItem = (MenuHeaderItem)item;
                        view = inflater.Inflate(Resource.Layout.ListViewHeaderItem, null);
                        // user dont click header item
                        view.Clickable = false;

                        var headerName = view.FindViewById<TextView>(Resource.Id.txtHeader);
                        headerName.Text = _headerItem.HeaderText;

                    }
                    else if (item.GetMenuItemsType() == TYPE_SEPORATOR)
                    {
                        MenuContentItem _contentItem = (MenuContentItem)item;
                        view = inflater.Inflate(Resource.Layout.ListViewContentItem, null);

                        var _title = view.FindViewById<TextView>(Resource.Id.txtTitle);
                        var _imgIcon = view.FindViewById<ImageView>(Resource.Id.imgIcon);
                        var _subTitle = view.FindViewById<TextView>(Resource.Id.txtSubTitle);

                        _title.Text = _contentItem.Title;
                        _imgIcon.SetBackgroundResource(_contentItem.IconImage);
                        _subTitle.Text = _contentItem.SubTitle;
                    }
                }
                catch (Exception ex)
                {
                    Toast.MakeText(context, ex.Message, ToastLength.Long);
                }
                return view;
            }
        }
    }
}

我的控制台返回

[ViewRootImpl@1fcd26a[ServiziActivity]] ViewPostImeInputStage processPointer 0
[ViewRootImpl@1fcd26a[ServiziActivity]] ViewPostImeInputStage processPointer 1
app_sofis.Droid.ServiziActivity+MenuContentItem

【问题讨论】:

    标签: c# android xamarin xamarin.android


    【解决方案1】:

    您可以使用位置从列表中获取项目:

    lst.ItemClick += (object sender, Android.Widget.AdapterView.ItemClickEventArgs e) =>          
    {
        MenuItemsType selectedItem = (MenuItemsType)item[e.Position];
        Console.WriteLine(selectedItem.Title);
        Console.WriteLine(selectedItem.SubTitle);
    };
    

    希望对你有帮助

    【讨论】:

    • 错误:“ServiziActivity.IMenuItemsType”不包含“SubTitle”的定义,并且找不到接受“ServiziActivity.IMenuItemsType”类型的第一个参数的扩展方法“SubTitle”(您是否缺少使用指令还是程序集引用?)(CS1061)(app_sofis.Droid)
    • 标题和副标题在 MenuItemType 中,而不是在 IMenuItemType 中。有两种解决方案,或者创建一个 MenuItemType 列表而不是 IMenuItemType。或者只是将属性(标题、副标题)移动到 IMenutItemType。或将项目转换为 MenuItemType :检查我更新的答案。
    • with "MenuContentItem selectedItem = (MenuContentItem)item[e.Position];"很好用,谢谢
    • 是的,请更新代码 -> 使用“MenuContentItem selectedItem = (MenuContentItem)item[e.Position];”
    • 用“MenuContentItem”替换“MenuItemsType”,这样效果很好:)
    【解决方案2】:

    我建议在您的适配器类中添加以下内容:

    public override IMenuItemsType this [int index]
    {
        get { return items[index]; }
    }
    

    这样更容易访问给定位置的项目。

    lst.ItemClick += (object sender, Android.Widget.AdapterView.ItemClickEventArgs e) =>
    {
        var selectedFromList = (lst.Adapter as ListViewAdapter)[e.Position];
    
        if(selectedFromList is MenuHeaderItem)
        {
            var intent = new Intent(this, typeof(YOUR_ACTIVITY1));
    
            StartActivity(intent);
        }
    
        if(selectedFromList is MenuContentItem)
        {
            var intent = new Intent(this, typeof(YOUR_ACTIVITY2));
    
            StartActivity(intent);
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      • 2014-05-12
      • 2021-01-06
      • 2018-06-18
      • 2014-10-20
      相关资源
      最近更新 更多