0

I know there are many solutions provided to this issue, But I am not exactly the proper solution to this, when a checkbox is checked I am assigning a ID to it,and adding it to a array, but when scroll down, the checkbox gets unchecked ,but the ID is stored in array,I want to keep the checkbox checked if it is checked.

My adapter class is.

public class sendivitesadapter extends ArrayAdapter<Item> implements Filterable,SectionIndexer{
    private Context context;
    private ArrayList<Item> items;
    private qrusers qrusers;
    private LayoutInflater vi;
    private String[] udis;
    private final boolean[] mCheckedState;
    qrusers qrus;

    private ItemsFilter mFilter;

    public sendivitesadapter(Context context,ArrayList<Item> items,String[]udis) {
        super(context, 0,items);

        this.context = context;
        mCheckedState = new boolean[items.size()];
        this.qrusers =(qrusers) context;
        this.items = items;
        this.udis=uid;
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Item getItem(int position) {
        return super.getItem(position);
    }

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

        View v = convertView;

        final Item i = items.get(position);

        if (i != null) {
            if(i.isSection()){
                SectionItem si = (SectionItem)i;
                v = vi.inflate(R.layout.checkboxlist, null);

                v.setOnClickListener(null);
                v.setOnLongClickListener(null);
                v.setLongClickable(false);

                final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
                sectionView.setText(si.getTitle());

            }else{
                sendItem ei = (sendItem)i;
                v = vi.inflate(R.layout.checkboxlist, null);
                final TextView title = (TextView)v.findViewById(R.id.contactname);
                final TextView subtitle = (TextView)v.findViewById(R.id.companyname);
                 checkBox=(CheckBox)v.findViewById(R.id.checboxlist);
                //checkBox.setTag(position);
                //items.addAll(uid);

               checkBox.setTag(udis[position]);

               checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        s = (String)buttonView.getTag();
                        Log.e("IDDDDDDDD", s);

                        userid.add(s);
                        Log.e("Array", userid.toString());
                    } else {
                        s = (String)buttonView.getTag();
                        userid.remove(s);

                    }
                    SharedPreferences app_preferences = PreferenceManager
                            .getDefaultSharedPreferences(qrusers.this);
                    SharedPreferences.Editor editor = app_preferences.edit();

                    editor.putString("userid", TextUtils.join(",", userid));
                    editor.commit();
                }
            });


                //    Log.e("IDDDDDDD", text);
                    //checkBox.setTag("12");
                /*  checkBox.setOnClickListener(new OnClickListener() {


                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub

                        if(checkBox.isChecked()){
                             s= (String)v.getTag();
                            Log.e("IDDDDDDDDDDDDDDDDDDDDDD", s);
                        userid.add(s);

                        Log.e("Array", userid.toString());

                        }
                        else{
                        s=(String)v.getTag();
                        userid.remove(s);
                        }

                        }
                    });*/
                    if (title != null) 
                        title.setText(ei.contactname);
                    if(subtitle != null)
                        subtitle.setText(ei.companyname);

                }
            }
            return v;
        }
2
  • take one static hash map in that put your listview position and boolean value,so when you call adapter first time you will set all positions false.After that when you select any check box you will make that position with boolean value true,so when you scroll down also it won't make any change because static hashmap we are taking Commented Jan 23, 2014 at 9:31
  • First of all make use of BaseAdapter instead of ArrayAdapter & inside getView method of your adapter save check status of your checkbox. Commented Jan 23, 2014 at 10:43

2 Answers 2

1

If you want to store the status of the checkboxes, you'll have to update it in your internal ListView or whatever structure you're using and what seems to be called items in your extended adapter, and don't forget to call notifyDataSetChanged() afterwards.

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

Comments

0

This question has your answer. You have to maintain a separate structure to check/uncheck the checkboxes.

ListView Viewholder checkbox state

Comments

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.