-1

I have 20 items displayed in my ListView. All the data is coming from mysql database. I have a Button, in my onClickListener it will set the Text of that button. But when it does, it also set the Text of other items in the list. And when I scroll through the items in the list it randomly changes the text of other button. Some are reverted to the original text, and some was set to the Text that I put, and keeps on changing as I keep on scrolling. I tried taking the setText in and out of the if statement but still no luck.

Here is my Adapter

public class myOrderListAdapter extends ArrayAdapter<myOrder> {

    public class ViewHolder{
        TextView tableNum;
        TextView itemName;
        TextView itemQuantity;
        Button btnStatus;
    }

    public myOrderListAdapter(Context context, ArrayList<myOrder> orderList) {
        super(context, 0,orderList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final myOrder orderList = getItem(position);

        final ViewHolder viewHolder;

        if(convertView == null){
            viewHolder = new ViewHolder();

            convertView = LayoutInflater.from(getContext()).inflate(R.layout.order_list_layout,parent,false);

            viewHolder.tableNum = (TextView)convertView.findViewById(R.id.OLtableNum);
            viewHolder.itemName = (TextView)convertView.findViewById(R.id.OLitemName);
            viewHolder.itemQuantity = (TextView)convertView.findViewById(R.id.OLitemQuantity);
            viewHolder.btnStatus = (Button)convertView.findViewById(R.id.statusButton);

            convertView.setTag(viewHolder);
        }
        else {
            viewHolder = (ViewHolder)convertView.getTag();
        }

        viewHolder.tableNum.setText("Order for Table "+orderList.getTable_id());
        viewHolder.itemName.setText(orderList.getItem_name());
        viewHolder.itemQuantity.setText("QTY: " + orderList.getQuantity());

        if(orderList.getStatus_id() == 1){
            viewHolder.btnStatus.setText("Preparing");
            viewHolder.btnStatus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    viewHolder.btnStatus.setText("Served");
                }
            });
            notifyDataSetChanged();
        }



        return convertView;
    }
}

1 Answers1

0

The ListView recycles the ListView Items , if you have 10 items and the Current list shows 5 items when you scroll it ll give the 1st item as the 6th item . add logs to check to print the ID of the view in the if & else cases , you need to keep the logic of updating the text based on the recycled view.

How ListView's recycling mechanism works

Community
  • 1
  • 1
surya
  • 607
  • 5
  • 18
  • where should I set my text? i cannot understand the comment `//this is a recyled view you need to set your textview cause a` because it was complete – newBieUser0829 Sep 06 '16 at 15:58
  • If you create a new View holder always it will work .. //if(convertView == null){ -- your view holder creation and data update //} /*else{ }*/ Comment this code and check it will work. – surya Sep 06 '16 at 16:13
  • Can you please edit my codes, No offence, but i can't seems to catch what you are trying to tell me. – newBieUser0829 Sep 06 '16 at 16:18
  • It worked somehow. but when I try to scroll up once again it also reset the data changed from the previous items. – newBieUser0829 Sep 06 '16 at 16:42
  • it reset the items you changed . you mean you had changed the text for some items with some logic ?? then you need to understand that views recycle maintain the data for that separately and load them. – surya Sep 06 '16 at 16:45