0

My rows contain a button that has its own click listener set in my adapter's

When I click on a button it sets the button text "yes' and again click than change it to "No" properly, my problem is as I scroll through the list its setting it for different rows as well. I assume theirs an issue somewhere with views recycling. and when i scroll list then changed text button needs double click to again change button text. but i want it on single click. what should i have to do?

Here's my code:

public class ListAdapter extends ArrayAdapter<Model> {
    customButtonListener customListner;

    public interface customButtonListener {
        public void onButtonClickListner(int position, Model model);
    }

    public void setCustomButtonListner(customButtonListener listener) {
        this.customListner = listener;
    }

    private Context context;
    private ArrayList<Model> data = new ArrayList<Model>();

    public ListAdapter(Context context, ArrayList<Model> dataItem) {
        super(context, R.layout.row, dataItem);
        this.data = dataItem;
        this.context = context;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.row, null);
            viewHolder = new ViewHolder();
            viewHolder.text = (TextView) convertView
                    .findViewById(R.id.childTextView);
            viewHolder.text1 = (TextView) convertView
                    .findViewById(R.id.childTextView1);
            viewHolder.button = (Button) convertView
                    .findViewById(R.id.childButton);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final Model model = getItem(position);
        viewHolder.text.setText(model.getNames());
        viewHolder.button.setTag(1);
        viewHolder.button.setText(model.getYes());
        viewHolder.button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                int status = (Integer) v.getTag();
                if (status == 1) {
                    model.setYes("Yes");

                    viewHolder.button.setText("Yes");
                    v.setTag(0);
                } else {
                    model.setYes("No");

                    viewHolder.button.setText("No");
                    v.setTag(1);
                }
            }
        });


        return convertView;
    }

    public class ViewHolder {
        ViewHolder viewHolder;
        TextView text, text1;
        Button button;
    }

}
Blip
  • 3,061
  • 5
  • 22
  • 50

2 Answers2

0

you need to remove viewHolder.button.setTag(1); from code. This code actually changes the tag previously set to 0 to 1.So 2 click is required to change it again to 1 then change is reflected in ui.

Fabin Paul
  • 1,701
  • 1
  • 16
  • 18
  • when i remove it then button text didn't change as yes No yes no alternatlyyyy i want that when i first click yes then 2nd click No scroll down and again scroll up and then when i clcik on yes it takes double click when scroll then getview reset as starting state –  Sep 12 '15 at 06:59
0

This is a common problem in listview.You can overcome it with getViewTypeCount() and getItemViewType(int position) methods.

Add this two methods in your adapter like:

@Override
public int getViewTypeCount() {
    // TODO Auto-generated method stub
    return 2;//The number of conditions that may occur in listview
}


@Override
public int getItemViewType(int position) {
    // TODO Auto-generated method stub

     if (status == 1) {
           return 1;
        } else {
           return 0;
        }

}

Then in getView() method:

 int type=getItemViewType(position);


                if(type==1)
                {
                     model.setYes("Yes");

                viewHolder.button.setText("Yes");
                v.setTag(0);
                }else{
                     model.setYes("No");
                viewHolder.button.setText("No");
                v.setTag(1);
                }

To know more about this two methods you can see this and also this.

And tutorials: here and here.

This problem will not occur again if you correctly implement this 2 methods.

Community
  • 1
  • 1
kgandroid
  • 5,507
  • 5
  • 39
  • 69