jeudi 15 juin 2017

CheckBox checked/unchecked state changes on scroll listview BaseAdapter

I have the list view with some pre-checked checked box...when i use to uncheck the checked checkbox and scrolls the it again gets checked and if i check the unchecked the checkbox then scrolls it changed to unchecked state.

When i

- checked the checkbox and then scrolls it gets unchecked

- unchecked the pre checked checkbox and scrolls then it gets checked

public class Adapter extends BaseAdapter {

private Context activity;
private ArrayList<Data> data;
private static LayoutInflater inflater = null;
private View vi;
TextView roll;
TextView menu;
CheckBox checkBox;
CheckBox cb;
public Adapter(Context context, ArrayList<Data> items) {
    this.activity = context;
    this.data = items;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int i) {
    return data.get(i);
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    vi = view;
    final int pos = position;
    final Data items = data.get(pos);
    vi = inflater.inflate(R.layout.list_row, null);
    checkBox = (CheckBox) vi.findViewById(R.id.cb);
    menu = (TextView) vi.findViewById(R.id.nama_menu);
    roll = (TextView) vi.findViewById(R.id.roll);
    menu.setText(items.getMenu());
    roll.setText(items.getRoll());
    if (items.getRoll().equals(items.getRollBlocked())) {
        checkBox.setChecked(true);
        items.setCheckbox(true);
        notifyDataSetChanged();
        vi.setEnabled(false);
    }
    else if (items.getRoll().equals(items.getRollLate())) {
        checkBox.setChecked(true);
        items.setCheckbox(true);
        notifyDataSetChanged();
        vi.setEnabled(false);
    }

    else if (items.getRoll().equals(items.getRollCheck())) {

        checkBox.setChecked(true);
        items.setCheckbox(true);


    } else if (items.getRollBlocked().equals("-")&& items.getRollCheck().equals("-") && items.getRollLate().equals("-")) {
        checkBox.setChecked(false);
        items.setCheckbox(false);


    }

    vi.setTag(checkBox);
    vi.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final Data items = data.get(pos);
              cb = (CheckBox) v.getTag();
            if(items.getRoll().equals(items.getRollBlocked())){
                vi.setEnabled(false);
                cb.setChecked(true);
            }
            else if(items.getRoll().equals(items.getRollLate())){
                vi.setEnabled(false);
                cb.setChecked(true);
            }
            else if(items.getRoll().equals(items.getRollCheck())){

                if (cb.isChecked()) {
                    cb.setChecked(false);
                    items.setCheckbox(false);
                }else{
                    cb.setChecked(true);
                    items.setCheckbox(true);
                }

            }else if (items.getRollBlocked().equals("-") && items.getRollCheck().equals("-") && items.getRollLate().equals("-")){
                if (cb.isChecked()) {
                    cb.setChecked(false);
                    items.setCheckbox(false);

                }else{
                    cb.setChecked(true);
                    items.setCheckbox(true);
                }

            }else{

            }



        }
    });
    return vi;
}

public ArrayList<Data> getAllData(){
    return data;
}

}




Aucun commentaire:

Enregistrer un commentaire