3

I have use below code for maintain checkbox state but its not working. I want only two checkbox is checked.

public class CACompareList extends ArrayAdapter<CompareListData>{

    public static int count = 0;
    public LayoutInflater inflater;
    public static ArrayList<CompareListData> selectedId;
    public ArrayList<CompareListData> listObjects;
    Context contex;
    public CACompareList(Context context, int textViewResourceId,
            ArrayList<CompareListData> objects) {
        super(context, textViewResourceId, objects);
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.listObjects = objects;
        selectedId = new ArrayList<CompareListData>();
        this.contex = context;
    }
    public static class ViewHolder
    {   
        TextView txtViewLoanName;
        TextView txtViewHtmlString;
        CheckBox chkSelected;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if(convertView==null)
        {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.row_comparelist, null);

            holder.txtViewLoanName= (TextView) convertView.findViewById(R.id.rowcomparelist_tv_loanname);
            holder.txtViewHtmlString= (TextView) convertView.findViewById(R.id.rowcomparelist_tv_loandetail);
            holder.chkSelected= (CheckBox) convertView.findViewById(R.id.rowcomparelist_chk_selected);
            convertView.setTag(holder);
        }
        else{
            holder=(ViewHolder)convertView.getTag();
        }

        final CompareListData data = this.listObjects.get(position);

        holder.txtViewLoanName.setText(this.listObjects.get(position).getLoanName());
        holder.txtViewHtmlString.setText(Html.fromHtml(this.listObjects.get(position).getHtmlString()));


        holder.chkSelected.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                //listObjects.get(position).setSelected(isChecked);
                if(isChecked){
                    count++;

                }else{
                    count--;
                    selectedId.remove(data);
                }
                if(count >= 3)// it will allow 3 checkboxes only
                {
                    Toast.makeText(contex, "Select only two Loan", Toast.LENGTH_LONG).show();
                    buttonView.setChecked(false);
                    count--;
                }
                else
                {
                    selectedId.add(data);
                    buttonView.setSelected(isChecked);
                    int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                    listObjects.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                    Log.e("Selected Position is:", String.valueOf(getPosition));
                }
            }
        });
        holder.chkSelected.setTag(position);
        holder.chkSelected.setSelected(this.listObjects.get(position).getSelected());
        Log.e("Position is:", String.valueOf(position));
        return convertView;
    }

}

when i checked first checkbox and than scroll down and up I get different result my first checkbox is unchecked and another one is selected.

I have also checked on listView item click listner but it also doen't help me.

lvLoanLiat.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                if(IN_EDIT_MODE == false){
                    CompareListData data = (CompareListData)arg0.getItemAtPosition(arg2);

                    CheckBox cBox = (CheckBox) arg1.findViewById(R.id.rowcomparelist_chk_selected);
                    if(cBox.isChecked()){
                        count--;
                        selectedId.remove(data);
                    }else{
                        count++;
                    }
                    if(count >= 3)// it will allow 3 checkboxes only
                    {
                        Toast.makeText(CompareListActivity.this, "Select only two Loan", Toast.LENGTH_LONG).show();
                        cBox.setChecked(false);
                        count--;
                    }
                    else
                    {
                        selectedId.add(data);
                        cBox.setSelected(true);
                        data.setSelected(true);
                        Log.e("Selected Position is:", String.valueOf(arg2));
                    }
                }

            }
        });

Below is screen shots first i checked only first item as like below screenshots. enter image description here

Than I scroll Up and down than i get result like below image.

enter image description here

Thanks.

3
  • stackoverflow.com/questions/16685366/…. check this might help Commented Jun 18, 2013 at 6:33
  • 1
    @Raghunandan I have see it but it not help me. Commented Jun 18, 2013 at 6:41
  • why does it not help you? Commented Jun 18, 2013 at 6:44

2 Answers 2

1

In getView() you should unset the listener first

holder.chkSelected.setOnCheckedChangeListener(null);
holder.chkSelected.setSelected(isSelected);

and this maybe help you. Notice line 622.

Sign up to request clarification or add additional context in comments.

2 Comments

I have see it but can not help me still i get weired result.
@GirishBhutiya maybe you should put the setTag() before buttonView.getTag(), however i think it's not necessary to use setTag(position), the value getPosition alawys equals position
0

Have you tried using:

<activity name= ".YourActivity" android:configChanges="screenSize"/>

Hope this helps . Lemme know if doesn't.

2 Comments

I have problem in listView scroll not in activity.
The UI is bundled to the activity . Have you tried using the the said parameters . The parameters are used

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.