点击获取listview项目的数据

时间:2017-05-11 04:46:00

标签: android json listview xamarin xamarin.android

嘿伙计们我是xamarin android的新手,我创建了一个listview,它取得了json格式的价值,我想获得listview点击项目的名称或ID,我不知道如何......

这是我的班级

  class Doctors
    {
        public int id;

        public string Name;
        public string address;
        public string spec;
        public string rating;
        public string fee;


        public Doctors(string Name, string address, string spec, string rating, string fee)
        {
            this.Name = Name;
            this.address = address;
            this.spec = spec;
            this.rating = rating;
            this.fee = fee;
        }

    }

这是我的适配器

 class DoctorsAdapter : BaseAdapter<Doctors>
    {
        private Context mContext;
        private int mRowLayout;
        private List<Doctors> mFriends;


        public DoctorsAdapter(Context context, int rowLayout, List<Doctors> friends)
        {
            mContext = context;
            mRowLayout = rowLayout;
            mFriends = friends;

        }

        public override int Count
        {
            get { return mFriends.Count; }
        }

        public override Doctors this[int position]
        {
            get { return mFriends[position]; }
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View row = convertView;

            if (row == null)
            {
                row = LayoutInflater.From(mContext).Inflate(mRowLayout, parent, false);
            }




            TextView name = row.FindViewById<TextView>(Resource.Id.docname);
            name.Text = mFriends[position].Name;



            TextView spec = row.FindViewById<TextView>(Resource.Id.docspec);
            spec.Text = mFriends[position].spec;

            TextView address = row.FindViewById<TextView>(Resource.Id.docadd);
            address.Text = mFriends[position].address;

            TextView feeetv = row.FindViewById<TextView>(Resource.Id.feetv);
            feeetv.Text = mFriends[position].fee;

            RatingBar doctorrating = row.FindViewById<RatingBar>(Resource.Id.docrating);
            doctorrating.Rating = Int32.Parse(mFriends[position].rating);

            return row;
        }


    }

这是用json填充listview的方法

  private void populatelistview()
    {
        if (jsonString == null)
        {
            Toast.MakeText(this, "json is null", ToastLength.Long);

        }
        else
        {
            try
            {
                //  jsonobject = JObject.Parse(jsonString);
                jsonArray = JArray.Parse(jsonString);
                mDoctors = new List<Doctors>();
                int count = 0;
                while (count < jsonArray.Count)
                {
                    Doctors doctor = new Doctors(jsonArray[count]["Name"].ToString(), jsonArray[count]["spec"].ToString(), jsonArray[count]["address"].ToString(), jsonArray[count]["rating"].ToString(), jsonArray[count]["fees"].ToString());
                    //      Toast.MakeText(this, jsonArray[count]["name"].ToString(), ToastLength.Long).Show();
                    mDoctors.Add(doctor);
                    count++;
                }

                mListView = FindViewById<ListView>(Resource.Id.docview);
                mAdapter = new DoctorsAdapter(this, Resource.Layout.ticket_result, mDoctors);
                mListView.Adapter = mAdapter;
                mListView.ItemClick += MListView_ItemClick;
            }
            catch (System.Exception exception)
            {

                Toast.MakeText(this, exception.ToString(), ToastLength.Long);
            }


        }

1 个答案:

答案 0 :(得分:0)

  

我想获取listview点击项目的名称或ID

您可以像这样实施MListView_ItemClick

private void MListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
    var position = e.Position;
    var item = mDoctors[position] as Doctors;
    var name = item.Name;
    var id = item.id;
}

首先获取点击该项目的位置,然后在mDoctors中找到匹配的项目,最后您可以获得此项目中定义的Nameid。< / p>