【问题标题】:Mono for Android: ListView casting wrong values to toast适用于 Android 的 Mono:ListView 将错误的值转换为 toast
【发布时间】:2013-01-09 09:21:49
【问题描述】:

我的列表。ItemClick:

lstText.ItemClick += delegate (object sender, AdapterView.ItemClickEventArgs args) 
{
    var userIdTextView = FindViewById<TextView>(Resource.Id.txtUserId);
    var fetchUserId = userIdTextView.Text;

    sUserId = fetchUserId;

    Toast.MakeText(this, sUserId, ToastLength.Short).Show();

};

在列表视图中,(Resource.Id.txtUserId) 在每个列表项上都不同。当我单击 toast 时,它总是从列表中的第一项传递相同的值。我该如何解决?

请提供解决方案或提示

【问题讨论】:

    标签: android android-listview xamarin.android onitemclick


    【解决方案1】:

    FindViewById应该用于在listview上下文中查找每个项目的上下文中的资源id,即args.View

    lstText.ItemClick += delegate (object sender, AdapterView.ItemClickEventArgs args) 
    {
        var userIdTextView = args.View.FindViewById<TextView>(Resource.Id.txtUserId);
        var fetchUserId = userIdTextView.Text;
    
        sUserId = fetchUserId;
    
        Toast.MakeText(this, sUserId, ToastLength.Short).Show();
    
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      相关资源
      最近更新 更多