Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Implementing Custom ListView with EditText

$
0
0

I tried to create a custom listview with an edittext in each row, but whenever I put something inside the item, the value often replicates on other items, and sometimes the value of the edittext just disappears. My code is like this:

`class DataAdapter : BaseAdapter
    {

        private Context context;
        private ObservableCollection<DummyData> data;

        public DataAdapter(Context context, ObservableCollection<DummyData> data)
        {
            this.context = context;
            this.data = data;
        }


        public override Java.Lang.Object GetItem(int position)
        {
            return position;
        }

        public override long GetItemId(int position)
        {
            return data[position].Id;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var view = convertView;
            ViewHolder holder = null;

            if (view != null)
                holder = view.Tag as ViewHolder;

            if (holder == null)
            {
                holder = new ViewHolder();
                var inflater = context.GetSystemService(Context.LayoutInflaterService).JavaCast<LayoutInflater>();
                //replace with your item and your holder items
                //comment back in
                //view = inflater.Inflate(Resource.Layout.item, parent, false);
                view = inflater.Inflate(Resource.Layout.DataLayout, parent, false);

                //holder.Title = view.FindViewById<TextView>(Resource.Id.text);
                holder.IdTxtView = view.FindViewById<TextView>(Resource.Id.idTextView);
                holder.NameTxtView = view.FindViewById<TextView>(Resource.Id.nameTextView);
                holder.ThisEditText = view.FindViewById<EditText>(Resource.Id.thisEditText);

                view.Tag = holder;
            }


            //fill in your items
            //holder.Title.Text = "new text here";
            holder.NameTxtView.Text = data[position].Name;
            holder.IdTxtView.Text = data[position].Id.ToString();
            holder.ThisEditText.AfterTextChanged += (object sender, AfterTextChangedEventArgs e) =>
            {
                data[position].Value = Convert.ToInt32(holder.ThisEditText.Text);
            };



            return view;
        }

        //Fill in cound here, currently 0
        public override int Count
        {
            get
            {
                return data.Count;
            }
        }

    }

    class ViewHolder : Java.Lang.Object
    {
        //Your adapter views to re-use
        //public TextView Title { get; set; }
        public TextView IdTxtView { get; set; }
        public TextView NameTxtView { get; set; }
        public EditText ThisEditText { get; set; }
    }`

How can I achieve what I want?


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>