Xamarin.Android从ListView单击事件中获取对象属性

时间:2014-08-28 14:30:19

标签: c# listview xamarin xamarin.android

我是C#(几周)和Xamarin(大约一周)的新手。

我能够从教程中实现ListView适配器"在Android上的ListView中显示实体集合(//来自http://diptimayapatra.wordpress.com/2013/07/08/xamarin-display-entity-collection-in-listview-in-android/

我现在的问题是我不知道如何处理TextView文本上的click事件。

我的适配器的GetView代码是:

public override  View GetView(int position, View convertView, ViewGroup parent)
{
    var incident = incidents[position];
    View view = convertView;
    if(view == null)
    {
        view = context.LayoutInflater.Inflate(
            Resource.Layout.ListViewTemplate, null);
    }

    view.FindViewById<TextView>(Resource.Id.tvIncident).Text = 
        string.Format("{0}", incident.title);

    view.FindViewById<TextView>(Resource.Id.tvIncidentDescription).Text = 
        string.Format("{0}", incident.description);

    return view;
}

我的事件对象代码是:

public class Incident
{
   public int id {get; set;}
   public string title {get; set;}
   public string description {get; set;}
   public double latitude {get; set;}
   public double longitude {get; set;}
}

然后活动中的代码是

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.Main);
    listView = FindViewById<ListView>(Resource.Id.list);

    IncidentGet incGet = new IncidentGet();
    List<Incident> incidents = incGet.GetIncidentData()

    listAdapter = new ListViewAdapter(this, incidents);
    listView.Adapter = listAdapter;

    listView.ItemClick += listView_ItemClick;
}

然后

void listView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
    //have no idea how to get the properties of each Incident object here
}

我不确定listView_ItemClick是否可行,或者是否有其他方式。任何建议将不胜感激

2 个答案:

答案 0 :(得分:8)

您订阅的事件有一些不错的参数。如果您已经探究了AdapterView.ItemClickEventArgs中的内容,则会显示有Position属性,这基本上可以让您从Adapter获取项目,点击后View {1}}代表。

所以基本上你可以得到类似的事件:

void listView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
    var incident = incidents[e.Position];
    // do whatever with that incident here...
}

答案 1 :(得分:0)

如何通过listview在xamarin

中点击项目将数据传递给另一个
protected override void OnCreate (Bundle bundle)
        listView.ItemClick+= delegate(object sender, AdapterView.ItemClickEventArgs position)
        {    
            String selectedFromList =(String) (listView.GetItemAtPosition(position.Position));
            Intent i =new Intent(this,typeof(RedirectClass));
//          i.PutExtra("key",selectedFromList);
//          StartActivity(i);

            //int pos=Convert.ToInt32(position);
            //ListView Clicked item value
            //string  itemValue    =(string)listView.GetItemAtPosition(pos);

            //Toast.MakeText(this," position is "   +itemValue,ToastLength.Long).Show();

        };
    }
}

获取数据的第二项活动

TextView txt = FindViewById<TextView> (Resource.Id.textView1);


//  Intent intent = Intent.getIntent();

string str = Intent.GetStringExtra("key").ToString();

Toast.MakeText (this, " the data is recvied from main is.." + str, ToastLength.Long).Show ();
txt.Text = str.ToString ();