Issue with ViewHolder selection

别来无恙 提交于 2019-12-04 06:56:23

问题


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());`

回答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());


来源:https://stackoverflow.com/questions/26886799/issue-with-viewholder-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!