Switch changes its behaviour when there are more than 5 items in RecyclerView












0















I have recyclerView showing list of items from database. In every item there is a switch showing value true or false depending on database value.
If you change switch state (e.g turn it on/off) it will update value in database with current boolean. What is the problem, that it works well when there are up to 5 items in RecyclerView. If there are more than 5 items Switches of all items change its state to on/randomly and updating the row in database. When I tested by showing just Toast with current state it all worked fine.

I tested on 2 databases with same result: both sqlite3 with ContentProviders and now I migrated to Room with LiveData.



This is how I set onCheckedChanged in my adapter:



  @NonNull
@Override
public AlarmViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.alarm_item, parent, false);
return new AlarmViewHolder(view);
}



@Override
public void onBindViewHolder(@NonNull AlarmViewHolder holder, int position) {
//get data for current adapter position
Alarms currentData = mAlarms.get(position);
//check if alarm is valid and then set this value to the switch
holder.isAlarmActiveSwitch.setChecked(currentData.isValid());
new validationUpdater().switchChangedListener
(holder.isAlarmActiveSwitch,
context, currentData.get_id());`
}


I create instance of validationUpdater() class and call the method switchChangedListener, pass holder.isAlarmActiveSwitch as Switch, context and id of row in Database.



switchChangedListener() method:



public void switchChangedListener(@NotNull final Switch switchActive, @NotNull final Context context, final long id) {
switchActive.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(final CompoundButton compoundButton, final boolean b) {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
AlarmDatabase db = AlarmDatabase.getInstance(context);
AlarmsDao dao = db.alarmsDao();
dao.updateAlarm(id, b);
}
});

}
});
}


and @Query from Dao:



@Query("UPDATE alarm_table SET valid = :valid WHERE _id = :id") 
void updateAlarm(final long id, final boolean valid);


Could anyone explain me why this doesn't work as supposed only after more than 5 items are displayed?










share|improve this question

























  • i'm not sure, but since you are using RecyclerView, i guess you are doing wrong with OnCheckChangeListener. maybe you are setting OnCheckChangeListener at onCreateViewHolder and when ViewHolder has been reused, it uses previous data to update database.

    – yeonseok.seo
    Nov 27 '18 at 6:01











  • @yeonseok.seo I've updated my adapter code. I'm setting onCheckChangeListener inside onBindViewHolder method

    – Domin
    Nov 27 '18 at 10:50






  • 1





    hope this resolve after overrieding the getItemViewType method @Override public int getItemViewType(int position) { return position; }

    – Zahoor Saleem
    Nov 27 '18 at 11:13











  • @ZahoorSaleem please post your comment as the answer, it did work for me. Also could you provide me any logical explanation how it worked? I read documentation for this method, it is clear for me but I don't understand why it changed behaviour of Switch.

    – Domin
    Nov 27 '18 at 12:22
















0















I have recyclerView showing list of items from database. In every item there is a switch showing value true or false depending on database value.
If you change switch state (e.g turn it on/off) it will update value in database with current boolean. What is the problem, that it works well when there are up to 5 items in RecyclerView. If there are more than 5 items Switches of all items change its state to on/randomly and updating the row in database. When I tested by showing just Toast with current state it all worked fine.

I tested on 2 databases with same result: both sqlite3 with ContentProviders and now I migrated to Room with LiveData.



This is how I set onCheckedChanged in my adapter:



  @NonNull
@Override
public AlarmViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.alarm_item, parent, false);
return new AlarmViewHolder(view);
}



@Override
public void onBindViewHolder(@NonNull AlarmViewHolder holder, int position) {
//get data for current adapter position
Alarms currentData = mAlarms.get(position);
//check if alarm is valid and then set this value to the switch
holder.isAlarmActiveSwitch.setChecked(currentData.isValid());
new validationUpdater().switchChangedListener
(holder.isAlarmActiveSwitch,
context, currentData.get_id());`
}


I create instance of validationUpdater() class and call the method switchChangedListener, pass holder.isAlarmActiveSwitch as Switch, context and id of row in Database.



switchChangedListener() method:



public void switchChangedListener(@NotNull final Switch switchActive, @NotNull final Context context, final long id) {
switchActive.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(final CompoundButton compoundButton, final boolean b) {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
AlarmDatabase db = AlarmDatabase.getInstance(context);
AlarmsDao dao = db.alarmsDao();
dao.updateAlarm(id, b);
}
});

}
});
}


and @Query from Dao:



@Query("UPDATE alarm_table SET valid = :valid WHERE _id = :id") 
void updateAlarm(final long id, final boolean valid);


Could anyone explain me why this doesn't work as supposed only after more than 5 items are displayed?










share|improve this question

























  • i'm not sure, but since you are using RecyclerView, i guess you are doing wrong with OnCheckChangeListener. maybe you are setting OnCheckChangeListener at onCreateViewHolder and when ViewHolder has been reused, it uses previous data to update database.

    – yeonseok.seo
    Nov 27 '18 at 6:01











  • @yeonseok.seo I've updated my adapter code. I'm setting onCheckChangeListener inside onBindViewHolder method

    – Domin
    Nov 27 '18 at 10:50






  • 1





    hope this resolve after overrieding the getItemViewType method @Override public int getItemViewType(int position) { return position; }

    – Zahoor Saleem
    Nov 27 '18 at 11:13











  • @ZahoorSaleem please post your comment as the answer, it did work for me. Also could you provide me any logical explanation how it worked? I read documentation for this method, it is clear for me but I don't understand why it changed behaviour of Switch.

    – Domin
    Nov 27 '18 at 12:22














0












0








0








I have recyclerView showing list of items from database. In every item there is a switch showing value true or false depending on database value.
If you change switch state (e.g turn it on/off) it will update value in database with current boolean. What is the problem, that it works well when there are up to 5 items in RecyclerView. If there are more than 5 items Switches of all items change its state to on/randomly and updating the row in database. When I tested by showing just Toast with current state it all worked fine.

I tested on 2 databases with same result: both sqlite3 with ContentProviders and now I migrated to Room with LiveData.



This is how I set onCheckedChanged in my adapter:



  @NonNull
@Override
public AlarmViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.alarm_item, parent, false);
return new AlarmViewHolder(view);
}



@Override
public void onBindViewHolder(@NonNull AlarmViewHolder holder, int position) {
//get data for current adapter position
Alarms currentData = mAlarms.get(position);
//check if alarm is valid and then set this value to the switch
holder.isAlarmActiveSwitch.setChecked(currentData.isValid());
new validationUpdater().switchChangedListener
(holder.isAlarmActiveSwitch,
context, currentData.get_id());`
}


I create instance of validationUpdater() class and call the method switchChangedListener, pass holder.isAlarmActiveSwitch as Switch, context and id of row in Database.



switchChangedListener() method:



public void switchChangedListener(@NotNull final Switch switchActive, @NotNull final Context context, final long id) {
switchActive.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(final CompoundButton compoundButton, final boolean b) {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
AlarmDatabase db = AlarmDatabase.getInstance(context);
AlarmsDao dao = db.alarmsDao();
dao.updateAlarm(id, b);
}
});

}
});
}


and @Query from Dao:



@Query("UPDATE alarm_table SET valid = :valid WHERE _id = :id") 
void updateAlarm(final long id, final boolean valid);


Could anyone explain me why this doesn't work as supposed only after more than 5 items are displayed?










share|improve this question
















I have recyclerView showing list of items from database. In every item there is a switch showing value true or false depending on database value.
If you change switch state (e.g turn it on/off) it will update value in database with current boolean. What is the problem, that it works well when there are up to 5 items in RecyclerView. If there are more than 5 items Switches of all items change its state to on/randomly and updating the row in database. When I tested by showing just Toast with current state it all worked fine.

I tested on 2 databases with same result: both sqlite3 with ContentProviders and now I migrated to Room with LiveData.



This is how I set onCheckedChanged in my adapter:



  @NonNull
@Override
public AlarmViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.alarm_item, parent, false);
return new AlarmViewHolder(view);
}



@Override
public void onBindViewHolder(@NonNull AlarmViewHolder holder, int position) {
//get data for current adapter position
Alarms currentData = mAlarms.get(position);
//check if alarm is valid and then set this value to the switch
holder.isAlarmActiveSwitch.setChecked(currentData.isValid());
new validationUpdater().switchChangedListener
(holder.isAlarmActiveSwitch,
context, currentData.get_id());`
}


I create instance of validationUpdater() class and call the method switchChangedListener, pass holder.isAlarmActiveSwitch as Switch, context and id of row in Database.



switchChangedListener() method:



public void switchChangedListener(@NotNull final Switch switchActive, @NotNull final Context context, final long id) {
switchActive.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(final CompoundButton compoundButton, final boolean b) {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
AlarmDatabase db = AlarmDatabase.getInstance(context);
AlarmsDao dao = db.alarmsDao();
dao.updateAlarm(id, b);
}
});

}
});
}


and @Query from Dao:



@Query("UPDATE alarm_table SET valid = :valid WHERE _id = :id") 
void updateAlarm(final long id, final boolean valid);


Could anyone explain me why this doesn't work as supposed only after more than 5 items are displayed?







java android android-recyclerview sqlite3 android-room






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 10:49







Domin

















asked Nov 26 '18 at 13:10









DominDomin

459115




459115













  • i'm not sure, but since you are using RecyclerView, i guess you are doing wrong with OnCheckChangeListener. maybe you are setting OnCheckChangeListener at onCreateViewHolder and when ViewHolder has been reused, it uses previous data to update database.

    – yeonseok.seo
    Nov 27 '18 at 6:01











  • @yeonseok.seo I've updated my adapter code. I'm setting onCheckChangeListener inside onBindViewHolder method

    – Domin
    Nov 27 '18 at 10:50






  • 1





    hope this resolve after overrieding the getItemViewType method @Override public int getItemViewType(int position) { return position; }

    – Zahoor Saleem
    Nov 27 '18 at 11:13











  • @ZahoorSaleem please post your comment as the answer, it did work for me. Also could you provide me any logical explanation how it worked? I read documentation for this method, it is clear for me but I don't understand why it changed behaviour of Switch.

    – Domin
    Nov 27 '18 at 12:22



















  • i'm not sure, but since you are using RecyclerView, i guess you are doing wrong with OnCheckChangeListener. maybe you are setting OnCheckChangeListener at onCreateViewHolder and when ViewHolder has been reused, it uses previous data to update database.

    – yeonseok.seo
    Nov 27 '18 at 6:01











  • @yeonseok.seo I've updated my adapter code. I'm setting onCheckChangeListener inside onBindViewHolder method

    – Domin
    Nov 27 '18 at 10:50






  • 1





    hope this resolve after overrieding the getItemViewType method @Override public int getItemViewType(int position) { return position; }

    – Zahoor Saleem
    Nov 27 '18 at 11:13











  • @ZahoorSaleem please post your comment as the answer, it did work for me. Also could you provide me any logical explanation how it worked? I read documentation for this method, it is clear for me but I don't understand why it changed behaviour of Switch.

    – Domin
    Nov 27 '18 at 12:22

















i'm not sure, but since you are using RecyclerView, i guess you are doing wrong with OnCheckChangeListener. maybe you are setting OnCheckChangeListener at onCreateViewHolder and when ViewHolder has been reused, it uses previous data to update database.

– yeonseok.seo
Nov 27 '18 at 6:01





i'm not sure, but since you are using RecyclerView, i guess you are doing wrong with OnCheckChangeListener. maybe you are setting OnCheckChangeListener at onCreateViewHolder and when ViewHolder has been reused, it uses previous data to update database.

– yeonseok.seo
Nov 27 '18 at 6:01













@yeonseok.seo I've updated my adapter code. I'm setting onCheckChangeListener inside onBindViewHolder method

– Domin
Nov 27 '18 at 10:50





@yeonseok.seo I've updated my adapter code. I'm setting onCheckChangeListener inside onBindViewHolder method

– Domin
Nov 27 '18 at 10:50




1




1





hope this resolve after overrieding the getItemViewType method @Override public int getItemViewType(int position) { return position; }

– Zahoor Saleem
Nov 27 '18 at 11:13





hope this resolve after overrieding the getItemViewType method @Override public int getItemViewType(int position) { return position; }

– Zahoor Saleem
Nov 27 '18 at 11:13













@ZahoorSaleem please post your comment as the answer, it did work for me. Also could you provide me any logical explanation how it worked? I read documentation for this method, it is clear for me but I don't understand why it changed behaviour of Switch.

– Domin
Nov 27 '18 at 12:22





@ZahoorSaleem please post your comment as the answer, it did work for me. Also could you provide me any logical explanation how it worked? I read documentation for this method, it is clear for me but I don't understand why it changed behaviour of Switch.

– Domin
Nov 27 '18 at 12:22












1 Answer
1






active

oldest

votes


















1














your problem is row item duplication in recycler view overried the getItemViewType method, hope this works for you.



@Override
public int getItemViewType(int position) {
return position;
}





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53481868%2fswitch-changes-its-behaviour-when-there-are-more-than-5-items-in-recyclerview%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    your problem is row item duplication in recycler view overried the getItemViewType method, hope this works for you.



    @Override
    public int getItemViewType(int position) {
    return position;
    }





    share|improve this answer




























      1














      your problem is row item duplication in recycler view overried the getItemViewType method, hope this works for you.



      @Override
      public int getItemViewType(int position) {
      return position;
      }





      share|improve this answer


























        1












        1








        1







        your problem is row item duplication in recycler view overried the getItemViewType method, hope this works for you.



        @Override
        public int getItemViewType(int position) {
        return position;
        }





        share|improve this answer













        your problem is row item duplication in recycler view overried the getItemViewType method, hope this works for you.



        @Override
        public int getItemViewType(int position) {
        return position;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 '18 at 4:34









        Zahoor SaleemZahoor Saleem

        32229




        32229
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53481868%2fswitch-changes-its-behaviour-when-there-are-more-than-5-items-in-recyclerview%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Create new schema in PostgreSQL using DBeaver

            Fotorealismo