简体   繁体   中英

ListView items shuffling when scrolling

I have the following code in my adapter. Each item consists of two images, and under them a checkbox for each image. I want to display a list of these items, but when I scroll up and down it seems like the first and last items are the same. And then after a while the other items would be out of order as well. I've looked at other questions regarding this issue and I have not been able to find why my ListView items are shuffling after I scroll down and up.

@Override
public View getView(final int position, View convertView, ViewGroup parent){
    View view = convertView;
    if(view == null){
        viewHolder = new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.wheel_list_item, null);

        viewHolder.decisionNum = (TextView)view.findViewById(R.id.decisionNumText);
        viewHolder.leftWheel = (ImageView)view.findViewById(R.id.leftWheel);
        viewHolder.rightWheel = (ImageView)view.findViewById(R.id.rightWheel);
        viewHolder.leftCheckBox = (CheckBox)view.findViewById(R.id.leftCheckBox);
        viewHolder.rightCheckBox = (CheckBox)view.findViewById(R.id.rightCheckBox);
        viewHolder.wheelTuple = list.get(position);
        view.setTag(viewHolder);
    }
    else{
        viewHolder = (ViewHolder)view.getTag();
    }

    viewHolder.decisionNum.setText(Integer.toString(position + 1));

    viewHolder.leftCheckBox.setClickable(false);
    viewHolder.rightCheckBox.setClickable(false);

    viewHolder.leftWheel.setImageBitmap(new WheelView(context, viewHolder.wheelTuple.left).getBitmapReduced());
    viewHolder.rightWheel.setImageBitmap(new WheelView(context, viewHolder.wheelTuple.right).getBitmapReduced());

    int id = context.getResources().getIdentifier("GREY", "color", context.getPackageName());
    int colorId=context.getResources().getColor(id);

    if(!viewHolder.wheelTuple.left.isChosen()){
        viewHolder.leftWheel.setColorFilter(colorId, PorterDuff.Mode.MULTIPLY);
        viewHolder.rightCheckBox.setChecked(true);
    }
    else{
        viewHolder.rightWheel.setColorFilter(colorId, PorterDuff.Mode.MULTIPLY);
        viewHolder.leftCheckBox.setChecked(true);
    }

    return view;
}

static class ViewHolder{
    TextView decisionNum;
    ImageView leftWheel;
    ImageView rightWheel;
    CheckBox leftCheckBox;
    CheckBox rightCheckBox;
    WheelListActivity.WheelTuple wheelTuple;
}

You have to use below two functions,

@Override
    public int getViewTypeCount() {
        if(searchList.size()==0){
            return 1;
        }
        return searchList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

And if your dataArrayList is having size 0, then remember to return 1 from getViewTypeCount(),Otherwise you will get ArrayIndexOutOfBoundsException. Refer the above code example.

try and use code:

Addiding getview() in boolean array

        if (mCheckedState[position])
                     viewHolder.leftCheckBox.setChecked(true);
                else
                     viewHolder.leftCheckBox.setChecked(false);

        if (mCheckedState[position])
                    viewHolder.rightCheckBox.setChecked(true);
                else
                    viewHolder.rightCheckBox.setChecked(false);

Declare variable and set length:

boolean[] mCheckedState;

 public class Adapter_Sandesh extends BaseAdapter {

        Context context;
        boolean[] mCheckedState;
        private LayoutInflater mInflater;


        public Adapter_Sandesh(FragmentActivity activity, String[] list_margdarshan) {
            context=activity;
            mCheckedState = new boolean[<your length>];


        }

       ...

       ...

       ...



     @Override
     public View getView(final int position, View convertView, ViewGroup parent){
     View view = convertView;
     if(view == null){
        viewHolder = new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.wheel_list_item, null);

        viewHolder.decisionNum = (TextView)view.findViewById(R.id.decisionNumText);
        viewHolder.leftWheel = (ImageView)view.findViewById(R.id.leftWheel);
        viewHolder.rightWheel = (ImageView)view.findViewById(R.id.rightWheel);
        viewHolder.leftCheckBox = (CheckBox)view.findViewById(R.id.leftCheckBox);
        viewHolder.rightCheckBox = (CheckBox)view.findViewById(R.id.rightCheckBox);
        viewHolder.wheelTuple = list.get(position);
        view.setTag(viewHolder);
    }
    else{
        viewHolder = (ViewHolder)view.getTag();
    }

    viewHolder.decisionNum.setText(Integer.toString(position + 1));

    viewHolder.leftCheckBox.setClickable(false);
    viewHolder.rightCheckBox.setClickable(false);



    if (mCheckedState[position])
                 viewHolder.leftCheckBox.setChecked(true);
            else
                 viewHolder.leftCheckBox.setChecked(false);

    if (mCheckedState[position])
                viewHolder.rightCheckBox.setChecked(true);
            else
                viewHolder.rightCheckBox.setChecked(false);


    viewHolder.leftWheel.setImageBitmap(new WheelView(context, viewHolder.wheelTuple.left).getBitmapReduced());
    viewHolder.rightWheel.setImageBitmap(new WheelView(context, viewHolder.wheelTuple.right).getBitmapReduced());

    int id = context.getResources().getIdentifier("GREY", "color", context.getPackageName());
    int colorId=context.getResources().getColor(id);

    if(!viewHolder.wheelTuple.left.isChosen()){
        viewHolder.leftWheel.setColorFilter(colorId, PorterDuff.Mode.MULTIPLY);
        viewHolder.rightCheckBox.setChecked(true);
    }
    else{
        viewHolder.rightWheel.setColorFilter(colorId, PorterDuff.Mode.MULTIPLY);
        viewHolder.leftCheckBox.setChecked(true);
    }

    return view;
}

static class ViewHolder{
    TextView decisionNum;
    ImageView leftWheel;
    ImageView rightWheel;
    CheckBox leftCheckBox;
    CheckBox rightCheckBox;
    WheelListActivity.WheelTuple wheelTuple;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM