0

I have an expandableList Adapter working pretty well, but something in baffling me. On each child lists I have 2 buttons and a textview. I would like to make it some that when I click one button (+) that the textview number goes up and when I click the other button (-) that the textview number goes down.

 public View getChildView(final int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, childPosition);


    if (convertView == null) {

        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }
    final TextView txtListChild = (TextView) convertView
            .findViewById(R.id.lblListItem);


Button btnMinus = (Button) convertView
            .findViewById(R.id.btnMinus);

  btnMinus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            LinearLayout ll = (LinearLayout) v.getParent();
            TextView tv1 = (TextView) ll.getChildAt(1);

            String Count = tv1.getText().toString();
            int num = Integer.parseInt(Count);
            num = num - 1;
            tv1.setText(Integer.toString(num));



        }

So the above code works as I would expect it, the button is clicked the textview goes down by one and we all is good.

But here is where I am getting confused. If I open another part of the list and look at another list of children one of the TextViews in that list has also changed! So in short clicking a button in one child is not only affecting the text view right next to it, but is also affecting a text view in a completely different header.

HackingAway
  • 55
  • 2
  • 8

1 Answers1

1

I think you are unaware of the view recycling mechanism. You can read about it here: How ListView's recycling mechanism works

Basically, when you scroll a ListView (or ExpandableListView), the view that just got off the screen on the top will be reused to create a view that needs to be shown on the bottom.

Community
  • 1
  • 1
Singed
  • 1,053
  • 3
  • 11
  • 28
  • Your probably right I did not know about this. I will have to read more and figure out how to avoid this. I am assuming that I have to create something like the holder class to be able to overcome this trouble. – HackingAway Mar 07 '15 at 00:27