ListView - Button changes text on same row but also change text on another row

The problem is when I click on itemTwo(button) it changes the itemNum text for that row and another row, and vise versa.
For example; on row 1: if i click on the button it changes the text for that row but also the text for row 6.

So row 1 and 6 changes each other same with row 2 and 7, and row 3 and 8.

When I scroll back up the text on row one goes default and row 2 changes to row one's previous text and went i go scroll down and go back up some of the rows text change without clicking on the button

Side Note: My list view has 7 rows but it only show 4 rows. ListView has a height of 196dp and each cell/row has a height of 49dp.

 public class Holder : Java.Lang.Object
                {
                public TextView itemName
                    public TextView itemNum
                    public Button itemOne;
                    public Button itemTwo;
                    public int pos;

                public void Initialize(View view)
                {
                itemName = view.FindViewById<TextView> (Resource.Id.itemNameTextView);
                    itemNum = view.FindViewById<TextView> (Resource.Id.itemNumTextView);
                    itemOne = view.FindViewById<Button> (Resource.Id.itemOneButton);
                    itemTwo = view.FindViewById<Button> (Resource.Id.itemTwoButton);
                    addGarment.Click += itemClick;
                }

                public void Bind(Activity context, string gn, int one, int two,  int num, int position)
                {
                    itemtName.Text = gn;
                    itemOne.Tag = one;
                    itemTwo.Tag = two;
                itemNum.Tag = num;
                    pos = position;
                }

                public void itemClick(object sender, EventArgs e)
                {
                    Button clickedButton = sender as Button;

                    int s = (int)clickedButton.Tag;

                    Console.WriteLine (s.ToString ());
                    switch ((int)clickedButton.Tag)
                    {
                    case 122:
                        itemNum.Text = "1";
                        break;
                    case 222:
                        itemNum.Text = "2";
                        break;
                    case 322:
                        itemNum.Text = "3";
                        break;
                    }
                }
            }


private class ItemListAdapter : BaseAdapter<Item>
        {
            private Activity activity;
            public List<Item> data;

            public ItemListAdapter(Activity activity, List<Item> data)
            {
                this.activity = activity;
                this.data = data;
            }
            public override Item this[int position]
            {
                get { return this.data [position]; }
            }

            public override int Count
            {
                get { return this.data.Count(); }
                //get {return 5;}
            }

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

            public override View GetView(int position, View convertView, ViewGroup parent)
            {
                Holder holder;

                if (convertView == null) 
                {
                    convertView = this.activity.LayoutInflater.Inflate (Resource.Layout.GarmentItem, null);
                    holder = new Holder ();
                    holder.Initialize (convertView);
                    convertView.Tag = holder;   
                }

            Item item = this.data [position];
            holder = (Holder)convertView.Tag;
                holder.Bind (this.activity, item.itemName, item.itemOneTag, item.itemTwoTag, itemNumTag, position);

                return convertView;    
            }

Best Answer

  • TheGreatOneTheGreatOne USMember ✭✭
    edited September 20 Accepted Answer

    @sijar said:
    Can you share your solution?

    You will need to keep track of the list. Either by making the list static or passing the list to Holder by reference. Then in itemClick() itemNum.Text = list[pos].itemName;

    public class Holder : Java.Lang.Object
    {
        public TextView itemName;
        public TextView itemNum
        public Button itemOne;
        public Button itemTwo;
        public int pos;
        public List<Item> data;
    
        public void Initialize(View view)
        {
            itemName = view.FindViewById<TextView> (Resource.Id.itemNameTextView);
            itemNum = view.FindViewById<TextView> (Resource.Id.itemNumTextView);
            itemOne = view.FindViewById<Button> (Resource.Id.itemOneButton);
            itemTwo = view.FindViewById<Button> (Resource.Id.itemTwoButton);
            addGarment.Click += itemClick;
        }
    
        public void Bind(Activity context, string gn, int one, int two,  int num, int position, ref List<Item> data)
        {
            itemtName.Text = gn;
            itemOne.Tag = one;
            itemTwo.Tag = two;
            intemNum.Tag = num;
            pos = position;
            this.data = data;
        }
    
        public void itemClick(object sender, EventArgs e)
        {
            Button clickedButton = sender as Button;
    
            int s = (int)clickedButton.Tag;
    
            Console.WriteLine (s.ToString ());
            switch ((int)clickedButton.Tag)
            {
            case 122:
                itemNum.Text = data[pos].someString;
                break;
            case 222:
                itemNum.Text = data[pos].someString2;
                break;
            case 322:
                itemNum.Text = data[pos].someString3;
                break;
            }
        }
    }
    
    
    private class ItemListAdapter : BaseAdapter<Item>
    {
        private Activity activity;
        public List<Item> data;
    
        public ItemListAdapter(Activity activity, List<Item> data)
        {
            this.activity = activity;
            this.data = data;
        }
        public override Item this[int position]
        {
            get { return this.data [position]; }
        }
    
        public override int Count
        {
            get { return this.data.Count(); }
            //get {return 5;}
        }
    
        public override long GetItemId(int position)
        {
            return 0;
        }
    
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            Holder holder;
    
            if (convertView == null) 
            {
                convertView = this.activity.LayoutInflater.Inflate (Resource.Layout.GarmentItem, null);
                holder = new Holder ();
                holder.Initialize (convertView);
                convertView.Tag = holder;   
            }
    
            Item item = this.data [position];
            holder = (Holder)convertView.Tag;
            holder.Bind (this.activity, item.itemName, item.itemOneTag, item.itemTwoTag, itemNumTag, position, data);
    
            return convertView;    
        }
    }
    

Answers

  • ConorMurphyConorMurphy USMember

    Did you come across a solution for this?

  • TheGreatOneTheGreatOne USMember ✭✭

    Yes I did

  • sijarsijar MXMember

    Can you share your solution?

  • TheGreatOneTheGreatOne USMember ✭✭
    edited September 20 Accepted Answer

    @sijar said:
    Can you share your solution?

    You will need to keep track of the list. Either by making the list static or passing the list to Holder by reference. Then in itemClick() itemNum.Text = list[pos].itemName;

    public class Holder : Java.Lang.Object
    {
        public TextView itemName;
        public TextView itemNum
        public Button itemOne;
        public Button itemTwo;
        public int pos;
        public List<Item> data;
    
        public void Initialize(View view)
        {
            itemName = view.FindViewById<TextView> (Resource.Id.itemNameTextView);
            itemNum = view.FindViewById<TextView> (Resource.Id.itemNumTextView);
            itemOne = view.FindViewById<Button> (Resource.Id.itemOneButton);
            itemTwo = view.FindViewById<Button> (Resource.Id.itemTwoButton);
            addGarment.Click += itemClick;
        }
    
        public void Bind(Activity context, string gn, int one, int two,  int num, int position, ref List<Item> data)
        {
            itemtName.Text = gn;
            itemOne.Tag = one;
            itemTwo.Tag = two;
            intemNum.Tag = num;
            pos = position;
            this.data = data;
        }
    
        public void itemClick(object sender, EventArgs e)
        {
            Button clickedButton = sender as Button;
    
            int s = (int)clickedButton.Tag;
    
            Console.WriteLine (s.ToString ());
            switch ((int)clickedButton.Tag)
            {
            case 122:
                itemNum.Text = data[pos].someString;
                break;
            case 222:
                itemNum.Text = data[pos].someString2;
                break;
            case 322:
                itemNum.Text = data[pos].someString3;
                break;
            }
        }
    }
    
    
    private class ItemListAdapter : BaseAdapter<Item>
    {
        private Activity activity;
        public List<Item> data;
    
        public ItemListAdapter(Activity activity, List<Item> data)
        {
            this.activity = activity;
            this.data = data;
        }
        public override Item this[int position]
        {
            get { return this.data [position]; }
        }
    
        public override int Count
        {
            get { return this.data.Count(); }
            //get {return 5;}
        }
    
        public override long GetItemId(int position)
        {
            return 0;
        }
    
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            Holder holder;
    
            if (convertView == null) 
            {
                convertView = this.activity.LayoutInflater.Inflate (Resource.Layout.GarmentItem, null);
                holder = new Holder ();
                holder.Initialize (convertView);
                convertView.Tag = holder;   
            }
    
            Item item = this.data [position];
            holder = (Holder)convertView.Tag;
            holder.Bind (this.activity, item.itemName, item.itemOneTag, item.itemTwoTag, itemNumTag, position, data);
    
            return convertView;    
        }
    }
    
  • sijarsijar MXMember

    @TheGreatOne Thank you!!

Sign In or Register to comment.