选择列表视图项

本文关键字:视图 列表 选择 | 更新日期: 2023-09-27 18:02:56

我创建了一个包含足球俱乐部的ListView。我想选择一个项目,它打开一个新的活动与所选项目的信息。

这是我的ListView:
base.OnCreate (bundle);
        clubs = new string[] {
            "AC Milan",
            "Borussia Dortmund",
            "Chelsea London",
            "FC Barcelona",
            "Manchester United",
            "Manchester City",
            "Real Madrid"
        };
        ListAdapter = new ArrayAdapter<String> (this, Android.Resource.Layout.SimpleListItem1, clubs);
    }

选择列表视图项

你将需要重写OnListItemClick从ListView获取选中的项目:

protected override void OnListItemClick(ListView listview, 
                                           View view, int pos, long id)
  {
     var selectedvalue = clubs[pos];
    // start new Activity here....
    var intent = new Intent(this, typeof(Your_Next_Activity));
    intent.PutExtra("selectedvalue", selectedvalue);
    StartActivity(intent);
  }
  1. 创建OnItemClickListener
  2. 获取选中项目
  3. 使用详细信息启动新活动。