-1

I am working in an app in which I handle a ListView which is composite with ViewHolder. In this ViewHolder, I have a CheckBox. Here is my code:

private class SettingsArrayAdapter extends ArrayAdapter<Object> {

    Context context;
    ArrayList<Object> values;

    RowSettingsViewHolder vh = null;
    BigRowSettingsViewHolder bvh = null;

    @Override
    public int getItemViewType(int position) {
        if (values.get(position) instanceof RowContent) {
            return SMALL_ROW_DESIGN;
        } else
            return BIG_ROW_DESIGN;
        // return super.getItemViewType(position);
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    public SettingsArrayAdapter(Context context, ArrayList<Object> values) {
        super(context, R.layout.row_settings, values);
        this.context = context;
        this.values = values;
    }

    // disable selection in title row
    @Override
    public boolean isEnabled(int position) {

        return (position == 2 || position == 4 || position == 8 || position == 9 || position == 13) ? false : true;
    }

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

        vh = null;
        bvh = null;
        String sharedPrefKey;
        // Log.i("VH", "vh tag: " +vh.toString());
        int type = getItemViewType(position);

        // big row excluded
        if (type == SMALL_ROW_DESIGN) {
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.row_settings, parent, false);
                vh = new RowSettingsViewHolder();
                vh.titleTV = (TextView) convertView.findViewById(R.id.settings_row_title);
                vh.subtitleTV = (TextView) convertView.findViewById(R.id.settings_row_subtitle);
                vh.disclosureIV = (ImageView) convertView.findViewById(R.id.settings_disclosureIV);
                vh.checkCB = (CheckBox) convertView.findViewById(R.id.settings_cB);
                vh.separatorLine = (View) convertView.findViewById(R.id.settings_listView_separatorLine);

                int id = Resources.getSystem().getIdentifier("btn_check_holo_light", "drawable", "android");
                if (id != 0)
                    vh.checkCB.setButtonDrawable(id);

                Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Decima Light.otf");
                vh.titleTV.setTypeface(tf);

                convertView.setTag(vh);

                Log.i("VH", "vh tag: " + vh.toString());

            } else {
                vh = (RowSettingsViewHolder) convertView.getTag();
                vh.checkCB.setOnCheckedChangeListener(null);
            }
            vh.checkCB.setFocusable(false);
            RowContent thisSetting = null;
            thisSetting = (RowContent) values.get(position);

            // set row values in view
            vh.titleTV.setText(thisSetting.getTitle());
            vh.disclosureIV.setImageDrawable(thisSetting.getDisclosureDrawable());

            // subtitle
            vh.subtitleTV.setText(thisSetting.getSubtitle());
            String title = vh.titleTV.getText().toString();
            String subtitle = vh.subtitleTV.getText().toString();
            if (subtitle.equals("NOTIFICATIONS") || subtitle.equals("MUSIC") || subtitle.equals("EMAIL") || subtitle.equals("SYSTEM")) {
                Typeface tfSubt = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Decima Bold.otf");
                vh.subtitleTV.setTypeface(tfSubt);
                vh.separatorLine.setBackgroundColor(Color.BLACK);
            } else {
                Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Decima Light.otf");
                vh.subtitleTV.setTypeface(tf);
                vh.separatorLine.setBackgroundColor(Color.LTGRAY);
            }

            // checkbox
            if (!thisSetting.needsCheckBox()) {
                vh.checkCB.setVisibility(View.INVISIBLE);
                vh.checkCB.setOnClickListener(null);
            } else {
                vh.checkCB.setVisibility(View.VISIBLE);

                if (title.equals(getResources().getString(R.string.Spotify))) {
                    vh.checkCB.setChecked(sharedPreferencesHandler_settings.isSpotifyActive());
                    sharedPrefKey = SharedPrefsHandler.spotifyKey;
                    vh.checkCB.setTag(sharedPrefKey);
                } else if (title.equals(getResources().getString(R.string.Google_Play))) {
                    vh.checkCB.setChecked(sharedPreferencesHandler_settings.isGooglePlayerActive());
                    sharedPrefKey = SharedPrefsHandler.googlePlayerKey;
                    vh.checkCB.setTag(sharedPrefKey);
                } else if (title.equals(getResources().getString(R.string.default_music_player))) {
                    vh.checkCB.setChecked(sharedPreferencesHandler_settings.isDefaultPlayerActive());
                    sharedPrefKey = SharedPrefsHandler.defaulPlayerKey;
                    vh.checkCB.setTag(sharedPrefKey);
                } else if (title.equals(getResources().getString(R.string.default_mail))) {
                    vh.checkCB.setChecked(sharedPreferencesHandler_settings.isDefaultEmailAppActive());
                    sharedPrefKey = SharedPrefsHandler.defaultEmailKey;
                    vh.checkCB.setTag(sharedPrefKey);
                } else if (title.equals(getResources().getString(R.string.Hotmail))) {
                    vh.checkCB.setChecked(sharedPreferencesHandler_settings.isHotmailActive());
                    sharedPrefKey = SharedPrefsHandler.hotmailKey;
                    vh.checkCB.setTag(sharedPrefKey);
                } else if (title.equals(getResources().getString(R.string.Gmail))) {
                    vh.checkCB.setChecked(sharedPreferencesHandler_settings.isGmailActive());
                    sharedPrefKey = SharedPrefsHandler.gmailKey;
                    vh.checkCB.setTag(sharedPrefKey);
                } else if (title.equals(getResources().getString(R.string.enable_disable_all_notif))) {
                    vh.checkCB.setChecked(sharedPreferencesHandler_settings.areAllNotifsActive());
                    sharedPrefKey = SharedPrefsHandler.allNotifsActiveKey;
                    vh.checkCB.setTag(sharedPrefKey);
                }

                vh.checkCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        // TODO Auto-generated method stub
                        // Log.i("TabSettingsActivity", "checked");
                        Log.i("VH-2", "vh tag: " + vh.toString());
                        sharedPreferencesHandler_settings.setSettingActive(SharedPrefsHandler.allNotifsActiveKey, vh.checkCB.isChecked());
                        Log.i("TabSettingsActivity", "checked: " + Boolean.toString(isChecked));
                        // Log.i("isChecked", vh.checkCB.getTag().toString());
                        Log.i("TabSettingsActivity", "vh tag: " + vh.checkCB.getTag().toString());
                        sharedPreferencesHandler_settings.setSettingActive(vh.checkCB.getTag().toString(), isChecked);
                        // Do something here.
                        Log.i("VH-ID", "vh tag: " + vh.toString());
                    }
                });

            }
        }

        // big row
        else {

            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.row_settings_big, parent, false);
                bvh = new BigRowSettingsViewHolder(convertView);

                convertView.setTag(bvh);
            }

            else {
                bvh = (BigRowSettingsViewHolder) convertView.getTag();
            }
            bvh.setRowData();
        }

        return convertView;
    }
}

What I am doing is stored in SharedPreferences the ViewHolder item values. When I put the tic in any CheckBox it seems that everything is ok, but if I use the scroll, the tick disappears, then I have been investigating and I did the next:

I put this line:

Log.i("VH-ID", "vh tag: " +vh.toString());

When it is creating the ViewHolder, like this I can get the ID of each ViewHolder, also I added the same line as above inside the listener method:

vh.checkCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        // Log.i("TabSettingsActivity", "checked");
        Log.i("VH-2", "vh tag: " + vh.toString());
        sharedPreferencesHandler_settings.setSettingActive(SharedPrefsHandler.allNotifsActiveKey, vh.checkCB.isChecked());
        Log.i("TabSettingsActivity", "checked: " + Boolean.toString(isChecked));
        // Log.i("isChecked", vh.checkCB.getTag().toString());
        Log.i("TabSettingsActivity", "vh tag: " + vh.checkCB.getTag().toString());
        sharedPreferencesHandler_settings.setSettingActive(vh.checkCB.getTag().toString(), isChecked);
        // Do something here.
        Log.i("VH-ID", "vh tag: " + vh.toString());
    }
});

The thing is that for example when the application starts I can see all the ViewHolder ID, but when I checked in anyone I cans see how the ID is different, so it is taking a different ViewHolder that which I checked, and I do not have idea why take a different ID (ViewHolder).

I made the changes which Sufian told me, this is the code, even so now I get a unfortunatly....

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

     vh = null;
     bvh = null;
     String sharedPrefKey;

     int type = getItemViewType(position);

     // big row excluded
     if(type == SMALL_ROW_DESIGN){
         if(convertView == null || convertView.getTag() == null){
             LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(R.layout.row_settings, parent, false);
                    vh = new RowSettingsViewHolder();
                    vh.titleTV = (TextView) convertView.findViewById(R.id.settings_row_title);
                    vh.subtitleTV = (TextView) convertView.findViewById(R.id.settings_row_subtitle);
                    vh.disclosureIV = (ImageView) convertView.findViewById(R.id.settings_disclosureIV);
                    vh.checkCB = (CheckBox) convertView.findViewById(R.id.settings_cB);
                    vh.separatorLine = (View)convertView.findViewById(R.id.settings_listView_separatorLine);


                    int id = Resources.getSystem().getIdentifier("btn_check_holo_light", "drawable", "android");
                    if(id!=0)
                        vh.checkCB.setButtonDrawable(id);

                    Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Decima Light.otf");
                    vh.titleTV.setTypeface(tf);

                    vh.checkCB.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            // TODO Auto-generated method stub
                            //Log.i("TabSettingsActivity", "checked");
                            sharedPreferencesHandler_settings.setSettingActive(SharedPrefsHandler.allNotifsActiveKey, vh.checkCB.isChecked());
                            Log.i("TabSettingsActivity", "checked: " +Boolean.toString(isChecked));
                            //Log.i("isChecked", vh.checkCB.getTag().toString());
                            Log.i("TabSettingsActivity", "vh tag: " +vh.checkCB.getTag().toString());
                            sharedPreferencesHandler_settings.setSettingActive(vh.checkCB.getTag().toString(), isChecked);
                            // Do something here.

                        }
                    });


                    convertView.setTag(vh);

                    Log.i("VH", "VH " +vh.toString());
                    Log.i("POSITION", "POSITION " +position);
            // init sahredPrefs if necessary
/*          if (sharedPrefsHandler == null) {
                SharedPrefsHandler.getInstance().initForNotifications(getActivity());
                sharedPrefsHandler = SharedPrefsHandler.getInstance();
            }*/


         }
         else{
             vh = (RowSettingsViewHolder) convertView.getTag();
             vh.checkCB.setTag(position);
             vh.checkCB.setChecked(vh.checkCB.isChecked());
         }

         RowContent thisSetting = null;
         thisSetting = (RowContent)values.get(position);

         // set row values in view 
         vh.titleTV.setText(thisSetting.getTitle());
         vh.disclosureIV.setImageDrawable(thisSetting.getDisclosureDrawable());

         // subtitle
         vh.subtitleTV.setText(thisSetting.getSubtitle());
         String title=vh.titleTV.getText().toString();
         String subtitle=vh.subtitleTV.getText().toString();
         if(subtitle.equals("NOTIFICATIONS") || subtitle.equals("MUSIC") || subtitle.equals("EMAIL") || subtitle.equals("SYSTEM")){
             Typeface tfSubt = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Decima Bold.otf");
             vh.subtitleTV.setTypeface(tfSubt);
             vh.separatorLine.setBackgroundColor(Color.BLACK);
         }
         else{
             Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Decima Light.otf");
             vh.subtitleTV.setTypeface(tf);
             vh.separatorLine.setBackgroundColor(Color.LTGRAY);
         }

         // checkbox         
         if(!thisSetting.needsCheckBox()){
             vh.checkCB.setVisibility(View.INVISIBLE); 
             vh.checkCB.setOnClickListener(null);
         }
         else{
             vh.checkCB.setVisibility(View.VISIBLE);



            if(title.equals(getResources().getString(R.string.Spotify))){
                 vh.checkCB.setChecked(sharedPreferencesHandler_settings.isSpotifyActive());
                 sharedPrefKey = SharedPrefsHandler.spotifyKey;
                 vh.checkCB.setTag(sharedPrefKey);
             }
             else if(title.equals(getResources().getString(R.string.Google_Play))){
                 vh.checkCB.setChecked(sharedPreferencesHandler_settings.isGooglePlayerActive());
                 sharedPrefKey = SharedPrefsHandler.googlePlayerKey;
                 vh.checkCB.setTag(sharedPrefKey);
             }
             else if(title.equals(getResources().getString(R.string.default_music_player))){
                 vh.checkCB.setChecked(sharedPreferencesHandler_settings.isDefaultPlayerActive());
                 sharedPrefKey = SharedPrefsHandler.defaulPlayerKey;
                 vh.checkCB.setTag(sharedPrefKey);
             }
             else if(title.equals(getResources().getString(R.string.default_mail))){
                 vh.checkCB.setChecked(sharedPreferencesHandler_settings.isDefaultEmailAppActive());
                 sharedPrefKey = SharedPrefsHandler.defaultEmailKey;
                 vh.checkCB.setTag(sharedPrefKey);
             }
             else if(title.equals(getResources().getString(R.string.Hotmail))){
                 vh.checkCB.setChecked(sharedPreferencesHandler_settings.isHotmailActive());
                 sharedPrefKey = SharedPrefsHandler.hotmailKey;
                 vh.checkCB.setTag(sharedPrefKey);
             }
             else if(title.equals(getResources().getString(R.string.Gmail))){
                 vh.checkCB.setChecked(sharedPreferencesHandler_settings.isGmailActive());
                 sharedPrefKey = SharedPrefsHandler.gmailKey;
                 vh.checkCB.setTag(sharedPrefKey);
             }
             else if(title.equals(getResources().getString(R.string.enable_disable_all_notif))){
                 vh.checkCB.setChecked(sharedPreferencesHandler_settings.areAllNotifsActive());
                 sharedPrefKey = SharedPrefsHandler.allNotifsActiveKey;
                 vh.checkCB.setTag(sharedPrefKey);
             }



        /*   vh.checkCB.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.i("TabSettingsActivity", "checked");
                        sharedPreferencesHandler_settings.setSettingActive(SharedPrefsHandler.allNotifsActiveKey, vh.checkCB.isChecked());
                        Log.i("isChecked", Boolean.valueOf(vh.checkCB.isChecked()).toString());
                        Log.i("isChecked", vh.checkCB.getTag().toString());
                        sharedPreferencesHandler_settings.setSettingActive(vh.checkCB.getTag().toString(), vh.checkCB.isChecked());
                        // Do something here.
                    }
                });  */
         }
     }

     //big row
     else {

         if(convertView == null){
         LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.row_settings_big, parent, false);
                bvh = new BigRowSettingsViewHolder(convertView);






                convertView.setTag(bvh); 
     }

         else{
             bvh = (BigRowSettingsViewHolder)convertView.getTag();
         }
         bvh.setRowData();           
     }

     return convertView;
 }

}

I hope this can help to solved the problem

Here is the LogCat:

11-25 13:19:48.671: D/dalvikvm(24077): GC_FOR_ALLOC freed 81K, 2% free 9068K/9180K, paused 15ms, total 15ms
11-25 13:19:48.671: I/dalvikvm-heap(24077): Grow heap (frag case) to 9.687MB for 842416-byte allocation
11-25 13:19:48.681: D/dalvikvm(24077): GC_FOR_ALLOC freed <1K, 2% free 9890K/10004K, paused 10ms, total 10ms
11-25 13:19:48.751: D/dalvikvm(24077): GC_FOR_ALLOC freed 24K, 1% free 10214K/10272K, paused 11ms, total 11ms
11-25 13:19:48.761: I/dalvikvm-heap(24077): Grow heap (frag case) to 17.913MB for 8294416-byte allocation
11-25 13:19:48.781: D/dalvikvm(24077): GC_FOR_ALLOC freed 1K, 1% free 18312K/18376K, paused 12ms, total 12ms
11-25 13:19:48.872: W/dalvikvm(24077): Unable to resolve superclass of Lcom/ionglasses2/ble/DfuService; (1377)
11-25 13:19:48.872: W/dalvikvm(24077): Link of class 'Lcom/ionglasses2/ble/DfuService;' failed
11-25 13:19:48.872: E/dalvikvm(24077): Could not find class 'com.ionglasses2.ble.DfuService', referenced from method com.ionglasses2.ble.DiscoveryService.startDFUService
11-25 13:19:48.872: W/dalvikvm(24077): VFY: unable to resolve const-class 1240 (Lcom/ionglasses2/ble/DfuService;) in Lcom/ionglasses2/ble/DiscoveryService;
11-25 13:19:48.872: D/dalvikvm(24077): VFY: replacing opcode 0x1c at 0x000c
11-25 13:19:48.882: I/TabNotificationsActivity(24077): get view EventosArrayAdapter
11-25 13:19:48.892: I/SharedPrefsHandler(24077): getInstance
11-25 13:19:48.902: I/SharedPrefsHandler(24077): initForNotifications
11-25 13:19:48.902: I/SharedPrefsHandler(24077): getInstance
11-25 13:19:48.902: I/SharedPrefsHandler(24077): Event: CALLS getIsEventActive: false
11-25 13:19:49.002: D/dalvikvm(24077): GC_FOR_ALLOC freed 425K, 3% free 20304K/20760K, paused 13ms, total 13ms
11-25 13:19:49.022: I/Adreno-EGL(24077): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
11-25 13:19:49.052: D/OpenGLRenderer(24077): Enabling debug mode 0
11-25 13:19:49.052: I/TabNotificationsActivity(24077): get view EventosArrayAdapter
11-25 13:19:49.062: I/SharedPrefsHandler(24077): Event: CALLS getIsEventActive: false
11-25 13:19:49.072: I/TabNotificationsActivity(24077): get view EventosArrayAdapter
11-25 13:19:49.072: I/SharedPrefsHandler(24077): Event: SMS getIsEventActive: false
11-25 13:19:49.082: I/TabNotificationsActivity(24077): get view EventosArrayAdapter
11-25 13:19:49.082: I/SharedPrefsHandler(24077): Event: EMAIL getIsEventActive: false
11-25 13:19:49.082: I/TabNotificationsActivity(24077): get view EventosArrayAdapter
11-25 13:19:49.092: I/SharedPrefsHandler(24077): Event: TWITTER getIsEventActive: false
11-25 13:19:52.175: I/SharedPrefsHandler(24077): getInstance
11-25 13:19:52.185: I/SharedPrefsHandler(24077): initForSettings
11-25 13:19:52.185: I/SharedPrefsHandler(24077): getInstance
11-25 13:19:52.185: I/TabSettings(24077): mState: 0
11-25 13:19:52.185: E/TabSettingsActivity(24077): onResume:RegistermGattUpdateReceiver
11-25 13:19:52.205: I/VH(24077): VH com.ionglasses2.TabSettingsActivity$RowSettingsViewHolder@42c22fa8
11-25 13:19:52.205: I/POSITION(24077): POSITION 0
11-25 13:19:52.225: I/VH(24077): VH com.ionglasses2.TabSettingsActivity$RowSettingsViewHolder@42c27b78
11-25 13:19:52.225: I/POSITION(24077): POSITION 1
11-25 13:19:52.235: I/VH(24077): VH com.ionglasses2.TabSettingsActivity$RowSettingsViewHolder@42c2bda0
11-25 13:19:52.235: I/POSITION(24077): POSITION 2
11-25 13:19:52.245: I/VH(24077): VH com.ionglasses2.TabSettingsActivity$RowSettingsViewHolder@42c2fff0
11-25 13:19:52.245: I/POSITION(24077): POSITION 3
11-25 13:19:52.245: I/SharedPrefs(24077): info leida are All notif Active: true
11-25 13:19:52.245: I/SharedPrefs(24077): allNotifsActive value: true
11-25 13:19:52.245: I/TabSettingsActivity(24077): checked: true
11-25 13:19:52.245: D/AndroidRuntime(24077): Shutting down VM
11-25 13:19:52.245: W/dalvikvm(24077): threadid=1: thread exiting with uncaught exception (group=0x41615ba8)
11-25 13:19:52.255: E/AndroidRuntime(24077): FATAL EXCEPTION: main
11-25 13:19:52.255: E/AndroidRuntime(24077): Process: com.ionglasses2, PID: 24077
11-25 13:19:52.255: E/AndroidRuntime(24077): java.lang.NullPointerException
11-25 13:19:52.255: E/AndroidRuntime(24077):    at com.ionglasses2.TabSettingsActivity$SettingsArrayAdapter$1.onCheckedChanged(TabSettingsActivity.java:1341)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.CompoundButton.setChecked(CompoundButton.java:127)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at com.ionglasses2.TabSettingsActivity$SettingsArrayAdapter.getView(TabSettingsActivity.java:1430)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.AbsListView.obtainView(AbsListView.java:2263)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.ListView.makeAndAddView(ListView.java:1790)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.ListView.fillDown(ListView.java:691)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.ListView.fillFromTop(ListView.java:752)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.ListView.layoutChildren(ListView.java:1630)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.AbsListView.onLayout(AbsListView.java:2091)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.View.layout(View.java:14817)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.View.layout(View.java:14817)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.View.layout(View.java:14817)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.View.layout(View.java:14817)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.View.layout(View.java:14817)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.View.layout(View.java:14817)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.View.layout(View.java:14817)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.View.layout(View.java:14817)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.Choreographer.doFrame(Choreographer.java:544)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.os.Handler.handleCallback(Handler.java:733)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.os.Handler.dispatchMessage(Handler.java:95)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.os.Looper.loop(Looper.java:136)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at android.app.ActivityThread.main(ActivityThread.java:5017)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at java.lang.reflect.Method.invokeNative(Native Method)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at java.lang.reflect.Method.invoke(Method.java:515)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-25 13:19:52.255: E/AndroidRuntime(24077):    at dalvik.system.NativeStart.main(Native Method)

Finally I resolved the problem this is the code:

`vh.checkCB.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            // TODO Auto-generated method stub
                            //Log.i("TabSettingsActivity", "checked");
                       //     sharedPreferencesHandler_settings.setSettingActive(SharedPrefsHandler.allNotifsActiveKey, vh.checkCB.isChecked());
                            Log.i("TabSettingsActivity", "checked: " +Boolean.toString(isChecked));
                         //   int rowPosition = (Integer)buttonView.getTag();
                            sharedPreferencesHandler_settings.setSettingActive(buttonView.getTag().toString(), isChecked);
                        //    Log.i("POSITION", "POSITION " +rowPosition);
                          //  getItem(rowPosition).setMarked(buttonView.isChecked());
                            //Log.i("isChecked", vh.checkCB.getTag().toString());
                      //      Log.i("TabSettingsActivity", "vh tag: " +vh.checkCB.getTag().toString());
                      //      sharedPreferencesHandler_settings.setSettingActive(vh.checkCB.getTag().toString(), isChecked);
                            // Do something here.

                        }
                    });


                    convertView.setTag(vh);

                    Log.i("VH", "VH " +vh.toString());
                    Log.i("POSITION", "POSITION " +position);
            // init sahredPrefs if necessary
/*          if (sharedPrefsHandler == null) {
                SharedPrefsHandler.getInstance().initForNotifications(getActivity());
                sharedPrefsHandler = SharedPrefsHandler.getInstance();
            }*/


         }
         else
             vh = (RowSettingsViewHolder) convertView.getTag();


         vh.checkCB.setTag(position);
         vh.checkCB.setChecked(vh.checkCB.isChecked());`
  • possible duplicate of [Checking a checkbox in listview makes other random checkboxes checked too](http://stackoverflow.com/questions/11190390/checking-a-checkbox-in-listview-makes-other-random-checkboxes-checked-too) – Sufian Nov 12 '14 at 13:13
  • Hi Sufian thanks for your quick answer, I tried this link before but it do not work for me. I do not why if I checked in any viwholder chekbox, it change but when I use the scroll, is cheked other, but this "other" is always the same viewholder – user4243681 Nov 12 '14 at 14:43
  • Did you try writing `vh.checkCB.setOnCheckedChangeListener(null)` in the else where you do `vh = (RowSettingsViewHolder) convertView.getTag();`? – Sufian Nov 13 '14 at 06:16
  • Hi Sufin yes I did, but I still have the problem, also I added the line holder.ckbox.setFocusable(false); but nothing I do not know where can be the problem to be honest – user4243681 Nov 17 '14 at 11:48
  • update your code in your question. – Sufian Nov 17 '14 at 11:50
  • Ok the updating is done – user4243681 Nov 17 '14 at 12:40
  • 1. Your **big row** doesn't have a `CheckBox`? 2. Your `CheckBox` states are recovering ok or they have problem with it too? – Sufian Nov 18 '14 at 06:01
  • 1. No it doesn´t have CheckBox 2. It seems that is working properly, it changes the state. The problem is when I do the scroll, up or down then dissapear the check – user4243681 Nov 18 '14 at 11:36
  • Please also answer the 2nd question - "2. Your CheckBox states are recovering ok or they have problem with it too?" – Sufian Nov 18 '14 at 11:39
  • it is not recovering properly, because when I move the scroll down and up, the checkbox values change. Is that what you mean rirght? – user4243681 Nov 18 '14 at 11:57

1 Answers1

1

I checked what I did when I faced similar issue. I actually set the OnCheckedChangeListener only once (when the row was created i.e as inflater.inflate()). And I set the row number as tag to the CheckBox.

Following is my code:

if (convertView == null || convertView.getTag() == null) {
    holder = new Holder();
    //TODO set holder fields here
    holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int rowPosition = (Integer)buttonView.getTag();
            getItem(rowPosition).setMarked(buttonView.isChecked());
        }
    });
    convertView.setTag(holder);
}
else
    holder = (Holder) convertView.getTag();
holder.checkBox.setTag(position);
holder.checkBox.setChecked(item.isChecked());
Sufian
  • 6,405
  • 16
  • 66
  • 120
  • Hi Sufian many thanks for your answer I just have one doubt, in this line `item.setMarkStateAt(rowPosition, buttonView.isChecked());` what I should put in item?? – user4243681 Nov 20 '14 at 13:14
  • Hi Sufian many thanks for your quick answer, sorry now I get an error: `The method setMarked(boolean) is undefined for the type Object` and what I should put in `holder.checkBox.setChecked(item.isChecked());` instead of item?? sorry Iam new whith this stuff. Many thanks – user4243681 Nov 20 '14 at 15:02
  • Of course you'll get this error because it is my `item`'s method. :) Try something like you have in your Question's `onCheckedChanged()`. – Sufian Nov 21 '14 at 06:07
  • @user4243681 fixes the problem? Don't forget to mark this answer. – Sufian Nov 22 '14 at 18:25
  • Hi Sufian, yeah dont worry I havent forgot mark the answer, but I did not because I have been testing and my applications stop when I enable or disable any checkbox, and the problem is this line `int rowPosition = (Integer)buttonView.getTag();` I do not know why, and also I do not have the `setOnCheckedChangeListener`inside of if (convertView == null || convertView.getTag() == null) {` but outside, I am not sure if this can be a problem aswll – user4243681 Nov 24 '14 at 13:45
  • @user4243681 1.put the `setOnCheckedChangeListener` line inside the `if()` like I have in the answer. 2. the crash is probably because you missed writing `holder.checkBox.setTag(position);`. Just double check if you haven't missed any line and are putting then in correct order/place. – Sufian Nov 24 '14 at 18:26
  • Hi Sufian, ok I just put the `setOnCheckedChangeListener`and when I want to go to this screen the app get the classic error unfortunaly.... I update my question with the modificate code – user4243681 Nov 25 '14 at 12:14
  • @user4243681 also post your Logcat – Sufian Nov 25 '14 at 12:15
  • @user4243681 Logcat is point to `TabSettingsActivity`'s line 1341. `com.ionglasses2.TabSettingsActivity$SettingsArrayAdapter$1.onCheckedChanged(TabSettingsActivity.java:1341)`. Could you point out the line? – Sufian Nov 25 '14 at 13:49
  • Hi Sufian, is this line `int rowPosition = (Integer)buttonView.getTag();` – user4243681 Nov 25 '14 at 15:27
  • But I tried to comment it, and I got the error in this line: `sharedPreferencesHandler_settings.setSettingActive(buttonView.getTag().toString(), isChecked);` then I think that the problem is when I try to get the buttonview Tag, that could be? – user4243681 Nov 25 '14 at 15:28
  • @user4243681 well, my code was setting the `position` in the `CheckBox`'s tag and I just noticed that you are also using the tag but for a different thing. Why do you need to store the key in the tag? Try moving it to your ViewHolder class (`RowSettingsViewHolder`). – Sufian Nov 26 '14 at 05:41
  • Hi Sufian, finally I got it!!! finally I realized that I had these lines inside of the `else` : `holder.checkBox.setTag(position);` and `holder.checkBox.setChecked(item.isChecked());` I edit my question with my final code. Many Thanks Sufian for your patience – user4243681 Nov 26 '14 at 11:28
  • @Sufian i am using base adapter with holder..and I am getting price from server using json,but i want to add indian rupee symbol before price,can you tell how to add –  Mar 16 '15 at 11:45
  • @Johnson please create a new question for that. You will need to post the JSON you're getting from server and also relevant code. Before that, try solving the problem yourself, while will also help you write a precise question. :) – Sufian Mar 16 '15 at 13:13
  • @Sufian see my question is here..http://stackoverflow.com/questions/29071143/how-to-add-indian-rupee-symbol –  Mar 16 '15 at 13:51