-1

hey guys i am new to xamarin android , i created a listview which takes value of json format i want to get the name or the id of the listview clicked item and i don't know how..

here is my class

  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;
        }

    }

and here is my adapter

 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;
        }


    }

and this the method which populates listview with json

  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);
            }


        }
  • Possible duplicate of [android listview get selected item](http://stackoverflow.com/questions/4508979/android-listview-get-selected-item) – Tim Klingeleers May 11 '17 at 06:26

1 Answers1

0

i want to get the name or the id of the listview clicked item

You can implement your MListView_ItemClick like this:

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;
}

First get the position where the item is clicked, then find the matched item in your mDoctors, and finally you can get the Name and id you defined in this item.

Grace Feng
  • 16,564
  • 2
  • 22
  • 45