繁体   English   中英

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

[英]Xamarin.Android get object properties from ListView click event

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

我能够从教程“在Android上的ListView中显示实体集合”(//来自http://diptimayapatra.wordpress.com/2013/07/08/xamarin-display-entity-collection-in-listview)实现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是否可行,或者是否有其他方式。 任何建议将不胜感激

您订阅的事件有一些很好的参数。 如果您已经探索了AdapterView.ItemClickEventArgs中的AdapterView.ItemClickEventArgs那么它将显示有一个Position属性,它基本上可以让您从Adapter获取项目,单击的View表示该项目。

所以基本上你可以得到一个事件:

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

如何通过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 (); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM