dimanche 28 juin 2015

Checkboxes in listview are reset onPause of android

What i have: Listview with checkboxes hosted in activity

What is happening:

  1. onPause happens and the screen restores the checkboxes are re-setted to their original state

  2. How to resolve this


In activity

AdptHomeDetail serialNumbers = new AdptHomeDetail(ActMyOrderDetail.this, result.getMyOrderDetails().getSerialNumbers());
lstVwId.setAdapter(serialNumbers);

In Adapter

AdptHomeDetail.java

public class AdptHomeDetail extends BaseAdapter {

    ArrayList<SerialNumbers> mSerialNumbers;

    private Context mContext = null;

    public AdptHomeDetail(Context context, ArrayList<SerialNumbers> serialNo) {
        super();
        mContext = context;
        mSerialNumbers = serialNo;

    }

    public int getCount() {
        return mSerialNumbers.size();
    }

    public SerialNumbers getItem(int position) {
        return mSerialNumbers.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

        View view = convertView;

        final ViewHolder vHolder;
        if (convertView == null) {
            LayoutInflater layout = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layout.inflate(R.layout.adp_act_myorder_detail, null);
            vHolder = new ViewHolder(view);
            view.setTag(vHolder);

            vHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                    mSerialNumbers.get(getPosition).setIsChecked(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                }
            });

            view.setTag(R.id.checkBox, vHolder.checkBox);

        } else {
            vHolder = (ViewHolder) view.getTag();
        }

        vHolder.checkBox.setTag(position); // This line is important.
        vHolder.checkBox.setChecked(mSerialNumbers.get(position).isChecked()); // Restore the maintained checkbox state

        vHolder.txtNameId.setText(mSerialNumbers.get(position).getProduct().getName()) ;
        vHolder.txtSlnoId.setText(mContext.getResources().getString(R.string.myOrderDetailSlnoTag) + " " + mSerialNumbers.get(position).getSerial_num() + "") ;

        return view;
    }

    class ViewHolder {

        private TextView txtNameId,txtSlnoId;
        CheckBox checkBox;

        public ViewHolder(View base) {


     txtNameId = (TextView) base.findViewById(R.id.txtNameId);
        txtSlnoId = (TextView) base.findViewById(R.id.txtSlnoId);
        checkBox = (CheckBox) base.findViewById(R.id.checkBox);
    }
}

}




Aucun commentaire:

Enregistrer un commentaire