Android setOnCheckedChangeListener runs twice - android

This is my code. What can ba a cause?
holder.favorite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
addFavorite(famous);
} else {
deleteFavorite(famous);
}
}
});
When isChecked=true, firstly is run addFavorite() and after that deleteFavorite().
When isChecked=false, firstly is run deleteFavorite() and after that addFavorite().
Don't know why...
EDIT: When I scroll down/up my ListView it calls these methods too... Weird...
private void deleteFavorite(final FamousTop40Ranking famous) {
DeleteFavoriteData data = new DeleteFavoriteData(famous.getId());
FavoriteDeleteApi.Factory.getInstance().deleteFavorite(data.getData())
.enqueue(new Callback<StatusInfoModel>() {
#Override
public void onResponse(Call<StatusInfoModel> call, Response<StatusInfoModel> response) {
showToast(mActivity, "Famous deleted from your Favorites list.");
famous.setFollowersCountry(famous.getFollowersCountry() - 1);
famous.setFollowersWorld(famous.getFollowersWorld() - 1);
notifyDataSetChanged();
}
#Override
public void onFailure(Call<StatusInfoModel> call, Throwable t) {
Log.d("deleteFavorite", mActivity.getString(R.string.something_went_wrong) + t.getMessage());
}
});
}
private void addFavorite(final FamousTop40Ranking famous) {
FavoriteCountApi.Factory.getInstance().countFavorites()
.enqueue(new Callback<CountFavoriteModel>() {
#Override
public void onResponse(Call<CountFavoriteModel> call, Response<CountFavoriteModel> response) {
if (response.isSuccessful()) {
if (response.body().getCount() < 20) {
FavoriteAddApi.Factory.getInstance().addFavorite(String.valueOf(famous.getId()))
.enqueue(new Callback<StatusInfoModel>() {
#Override
public void onResponse(Call<StatusInfoModel> call, Response<StatusInfoModel> response) {
showToast(mActivity, "Famous added from your Favorites list.");
famous.setFollowersCountry(famous.getFollowersCountry() + 1);
famous.setFollowersWorld(famous.getFollowersWorld() + 1);
notifyDataSetChanged();
}
#Override
public void onFailure(Call<StatusInfoModel> call, Throwable t) {
Log.d("addFavorite", mActivity.getString(R.string.something_went_wrong) + t.getMessage());
}
});
} else {
showToast(mActivity, mActivity.getString(R.string.reached_max_favorites));
}
}
}
#Override
public void onFailure(Call<CountFavoriteModel> call, Throwable t) {
Log.d("countFavorite", mActivity.getString(R.string.something_went_wrong) + t.getMessage());
}
});
}
getView() method:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_view_ranking_famous_single_item, parent, false);
}
final FamousTop40Ranking famous = famousModelList.get(position);
holder = new ViewHolder(convertView);
holder.name.setText(famous.getName());
if (mTab == 0) { // RankingFamousMainFragment.TAB_FAMOUS
holder.followers.setText(String.valueOf(famous.getFollowersWorld()));
} else {
holder.followers.setText(String.valueOf(famous.getFollowersCountry()));
}
if (famous.getIsFavorite().get(0).getFavorite().equals("1")) {
holder.favorite.setChecked(true);
} else {
holder.favorite.setChecked(false);
}
Glide
.with(mActivity)
.load(famous.getPhoto())
.fallback(R.drawable.bg_gradient)
.error(R.drawable.bg_gradient)
.centerCrop()
.crossFade()
.into(holder.photo);
holder.favorite.setOnCheckedChangeListener(null);
holder.favorite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
addFavorite(famous);
} else {
deleteFavorite(famous);
}
}
});
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showToast(mActivity, mActivity.getString(R.string.famous_clicked, position));
}
});
return convertView;
}

In getView. Move setonCHecked(null) before the if/else.
The reason this is hapening is getView might get called alot of times, in this case i think it is called because when you change state(check/uncheck) it redraws the button. And in your getView you are calling setChecked which triggers the listener. Setting the listener to null before calling holder.favorite.setChecked(true/false) wont double trigger it.
About up/down issue- It is the same, when the view is of the screen it is deleted. When it appears again it calls getView whichi triggers everything due to holder.favorite.setChecked(true) activating the onCheckChangedLIstener
As an example:
holder.favorite.setOnCheckedChangeListener(null);
holder.favorite.setChecked(true);
holder.favourite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// handle your logic
}
});

I may be late for the party!.. but this is how I fix a double trigger on radio Buttons, in my case I have a RadioGroup, that helps with the behavior of a group of radio buttons.
When you click on a specific radio button you expect to trigger lambda expression in "setOnCheckedChangeListener", which is normal.
But when you "reset" the group and need that no radio button be selected, you usually call either:
radioGroup.clearCheck()
or
radioGroup.check(-1)
But when you do that, the listener receives 2 calls, the first is for the radio button that is going to be "unchecked",
the second call with the radio button id equals -1.
so what ai have done, and work to me is:
radioGroup.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->
//If not -1, means that is a radio button that was been clicked (don't know yet is checked or not)
if (checkedId > 0) {
//Get radio button element
val radio: RadioButton = group.findViewById(checkedId)
// Preguntar por si esta checked == true ?
if (radio.isChecked) {
when (checkedId) {
R.id.rb_1 -> {
viewModel.onSetPv(1)
}
R.id.rb_2 -> {
viewModel.onSetPv(2)
}
else -> false
}
}
}
})
Best regards!

Actually, it's not an issue! Why? Imagine you have a radio-group, and you select one of the options; if you rotate the device, the selected option should be stay selected. (It save the state. In fact, it works just like view-model)
But, when it could be a problem? At first state, when you didn't choose any option. How to solve it? Just add if (checkedId > 0) before you check the ids:
radioGroup.setOnCheckedChangeListener((radioGroup, checkedId) -> {
if (checkedId > 0) {
// here check the ids
}
});
The other situation that you can face a problem is, when you use a same radio-group for multitasks (like quiz app). In this situation, what I did was using view-model. I let the view-model take care of saving state and right after I check the Ids, I add radioGroup.check(-1); example:
radioGroup.setOnCheckedChangeListener((radioGroup, checkedId) -> {
if (checkedId > 0) {
// here check the ids
// use viewModel
radioGroup.check(-1);
}
});

Related

How to change switch conditions using Toggle button?

I have a program which creates a list of to-dos that allows the user to set a date/time for the app to notify them about it. There's a checkbox that says 'Notify me' which is supposed to schedule the notification. But, in the main RecyclerView of the list, there is also a toggle switch that allows the user to turn off/on the notification after they've saved it. Problem is, the toggle switch doesn't seem to be changing the notification state.
holder.notifSwitch.setChecked(journalModel.isNotify());
if(journalModel.getJournalDateNotify().getTime() > System.currentTimeMillis())
{
holder.notifSwitch.setVisibility(View.VISIBLE);
} else {
holder.notifSwitch.setVisibility(View.INVISIBLE);
}
holder.notifSwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!journalModel.isNotify())
{
createJournalFunction.updateJournal(realm, journalModel.getRealmJournalNo(),true);
} else
{
createJournalFunction.updateJournal(realm, journalModel.getRealmJournalNo(), false);
}
}
});
And here is the code for updating the Realm object:
public boolean updateJournal (Realm realm, final int realmJournalNo, final boolean isNotify){
success = false;
try{
realm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
final TblJournal tblJournal = realm.where(TblJournal.class).equalTo("realmJournalNo", realmJournalNo).findFirst();
tblJournal.setNotify(isNotify);
success = true;
}
});
}catch (RealmException e){
Log.i("INFO","update Retail Exception: "+e.toString());
success = false;
}finally {
return success;
}
}
As per your code your are not updating journalModel.isNotify= true or false in on click. journalModel.setIsNotify(true):/journalModel.setIsNotify(false):
holder.notifSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// status="true"; //edit here
switch_btn.setChecked(true);
} else {
// status="false"; //edit here
switch_btn.setChecked(false);
}
}
});
Everything looks fine, you just need to modify your onClick method.
holder.notifSwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox checkbox = (CheckBox)v;
createJournalFunction.updateJournal(realm, journalModel.getRealmJournalNo(),checkbox.isChecked());
}
});

Recyclerview items with checkbox gets distorted

I have a RecyclerView with items and all of them have a checkbox. The point is that you can only set one checkbox as checked (something like favourite). Everything works fine until there are enough items that you need to scroll the RecyclerView. But, when there's enough items to scroll, then some of the item's names disappear and none of the checkboxes work except one and it is all stuck. Also, I get the following exception:
"Cannot call this method while RecyclerView is computing a layout or scrolling"
This is what I am doing:
holder.getBinder().cbIsFavourite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mListener == null) {
return;
}
if (isChecked) {
for (Peem peemTmp : mPeemList) {
if (peemTmp.isFavourite()) {
peemTmp.setFavourite(false);
}
}
peem.setFavourite(true);
PeemAdapter.this.notifyDataSetChanged();
} else {
peem.setFavourite(false);
removeFavouriteShops(peem);
}
mListener.onFavouriteChecked(peem);
}
});
The error occurs on this line PeemAdapter.this.notifyDataSetChanged();
If I remove that line it is all okay, but multiple checkboxes can be selected which is not the idea.
Why not use RadioButton and RadioGroup ?
Else try to call notifyDataSetChanged() in a post Runnable, it will be called after layout computing. Something like this:
myView.post(new Runnable() {
#Override
public void run() {
notifyDataSetChanged();
}
});
EDITED:
holder.getBinder().cbIsFavourite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mListener == null) {
return;
}
if (isChecked) {
for (Peem peemTmp : mPeemList) {
if (peemTmp.isFavourite()) {
peemTmp.setFavourite(false);
}
}
peem.setFavourite(true);
holder.getBinder().cbIsFavourite.post(new Runnable() {
#Override
public void run() {
PeemAdapter.this.notifyDataSetChanged();
}
});
} else {
peem.setFavourite(false);
removeFavouriteShops(peem);
}
mListener.onFavouriteChecked(peem);
}
});

Android RecyclerView : notifyDataSetChanged() IllegalStateException

I'm trying to update the items of a recycleview using notifyDataSetChanged().
This is my onBindViewHolder() method in the recycleview adapter.
#Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
//checkbox view listener
viewHolder.getCheckbox().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//update list items
notifyDataSetChanged();
}
});
}
What I want to do is update the list items, after I check a checkbox. I get an illegal exception though: "Cannot call this method while RecyclerView is computing a layout or scrolling"
java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
at android.support.v7.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:1462)
at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onChanged(RecyclerView.java:2982)
at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:7493)
at android.support.v7.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:4338)
at com.app.myapp.screens.RecycleAdapter.onRowSelect(RecycleAdapter.java:111)
I also used notifyItemChanged(), same exception. Any secret way to update to notify the adapter that something changed?
You should move method 'setOnCheckedChangeListener()' to ViewHolder which is inner class on your adapter.
onBindViewHolder() is not a method that initialize ViewHolder.
This method is step of refresh each recycler item.
When you call notifyDataSetChanged(), onBindViewHolder() will be called as the number of each item times.
So If you notifyDataSetChanged() put into onCheckChanged() and initialize checkBox in onBindViewHolder(), you will get IllegalStateException because of circular method call.
click checkbox -> onCheckedChanged() -> notifyDataSetChanged() -> onBindViewHolder() -> set checkbox -> onChecked...
Simply, you can fix this by put one flag into Adapter.
try this,
private boolean onBind;
public ViewHolder(View itemView) {
super(itemView);
mCheckBox = (CheckBox) itemView.findViewById(R.id.checkboxId);
mCheckBox.setOnCheckChangeListener(this);
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(!onBind) {
// your process when checkBox changed
// ...
notifyDataSetChanged();
}
}
...
#Override
public void onBindViewHolder(YourAdapter.ViewHolder viewHolder, int position) {
// process other views
// ...
onBind = true;
viewHolder.mCheckBox.setChecked(trueOrFalse);
onBind = false;
}
You can just reset the previous listener before you make changes and you won't get this exception.
private CompoundButton.OnCheckedChangeListener checkedListener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Do your stuff
});;
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.checkbox.setOnCheckedChangeListener(null);
holder.checkbox.setChecked(condition);
holder.checkbox.setOnCheckedChangeListener(checkedListener);
}
Using a Handler for adding items and calling notify...() from this Handler fixed the issue for me.
I don't know well, but I also had same problem. I solved this by using onClickListner on checkbox
viewHolder.mCheckBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (model.isCheckboxBoolean()) {
model.setCheckboxBoolean(false);
viewHolder.mCheckBox.setChecked(false);
} else {
model.setCheckboxBoolean(true);
viewHolder.mCheckBox.setChecked(true);
}
notifyDataSetChanged();
}
});
Try this, this may help!
protected void postAndNotifyAdapter(final Handler handler, final RecyclerView recyclerView, final RecyclerView.Adapter adapter) {
handler.post(new Runnable() {
#Override
public void run() {
if (!recyclerView.isComputingLayout()) {
adapter.notifyDataSetChanged();
} else {
postAndNotifyAdapter(handler, recyclerView, adapter);
}
}
});
}
Found a simple solution -
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private RecyclerView mRecyclerView;
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
mRecyclerView = recyclerView;
}
private CompoundButton.OnCheckedChangeListener checkedChangeListener
= (compoundButton, b) -> {
final int position = (int) compoundButton.getTag();
// This class is used to make changes to child view
final Event event = mDataset.get(position);
// Update state of checkbox or some other computation which you require
event.state = b;
// we create a runnable and then notify item changed at position, this fix crash
mRecyclerView.post(new Runnable() {
#Override public void run() {
notifyItemChanged(position));
}
});
}
}
Here we create a runnable to notifyItemChanged for a position when recyclerview is ready to handle it.
When you have the Message Error:
Cannot call this method while RecyclerView is computing a layout or scrolling
Simple, Just do what cause the Exception in:
RecyclerView.post(new Runnable() {
#Override
public void run() {
/**
** Put Your Code here, exemple:
**/
notifyItemChanged(position);
}
});
At first I thought Moonsoo's answer (the accepted answer) wouldn't work for me because I cannot initialize my setOnCheckedChangeListener() in the ViewHolder constructor because I need to bind it each time so it gets an updated position variable. But it took me a long time to realize what he was saying.
Here is an example of the "circular method call" he is talking about:
public void onBindViewHolder(final ViewHolder holder, final int position) {
SwitchCompat mySwitch = (SwitchCompat) view.findViewById(R.id.switch);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
data.delete(position);
notifyItemRemoved(position);
//This will call onBindViewHolder, but we can't do that when we are already in onBindViewHolder!
notifyItemRangeChanged(position, data.size());
}
}
});
//Set the switch to how it previously was.
mySwitch.setChecked(savedSwitchState); //If the saved state was "true", then this will trigger the infinite loop.
}
The only problem with this, is that when we need to initialize the switch to be on or off (from past saved state, for example), it is calling the listener which might call nofityItemRangeChanged which calls onBindViewHolder again. You cannot call onBindViewHolder when you are already in onBindViewHolder], because you cannot notifyItemRangeChanged if you are already in the middle of notifying that the item range has changed. But I only needed to update the UI to show it on or off, not wanting to actually trigger anything.
Here is the solution I learned from JoniDS's answer that will prevent the infinite loop. As long as we set the listener to "null" before we set Checked, then it will update the UI without triggering the listener, avoiding the infinite loop. Then we can set the listener after.
JoniDS's code:
holder.checkbox.setOnCheckedChangeListener(null);
holder.checkbox.setChecked(condition);
holder.checkbox.setOnCheckedChangeListener(checkedListener);
Full solution to my example:
public void onBindViewHolder(final ViewHolder holder, final int position) {
SwitchCompat mySwitch = (SwitchCompat) view.findViewById(R.id.switch);
//Set it to null to erase an existing listener from a recycled view.
mySwitch.setOnCheckedChangeListener(null);
//Set the switch to how it previously was without triggering the listener.
mySwitch.setChecked(savedSwitchState); //If the saved state was "true", then this will trigger the infinite loop.
//Set the listener now.
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
data.delete(position);
notifyItemRemoved(position);
//This will call onBindViewHolder, but we can't do that when we are already in onBindViewHolder!
notifyItemRangeChanged(position, data.size());
}
}
});
}
your CheckBox item is in changing drawable when you call notifyDataSetChanged(); so this exception would be occurred.
Try call notifyDataSetChanged(); in post of your view. For Example:
buttonView.post(new Runnable() {
#Override
public void run() {
notifyDataSetChanged();
}
});
Why not checking the RecyclerView.isComputingLayout() state as follows?
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private RecyclerView mRecyclerView;
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
mRecyclerView = recyclerView;
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.getCheckbox().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mRecyclerView != null && !mRecyclerView.isComputingLayout()) {
notifyDataSetChanged();
}
}
});
}
}
While item is being bound by the layout manager, it is very likely that you are setting the checked state of your checkbox, which is triggering the callback.
Of course this is a guess because you did not publish the full stack trace.
You cannot change adapter contents while RV is recalculating the layout. You can avoid it by not calling notifyDataSetChanged if item's checked state is equal to the value sent in the callback (which will be the case if calling checkbox.setChecked is triggering the callback).
Use onClickListner on checkbox instead of OnCheckedChangeListener, It will solve the problem
viewHolder.myCheckBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (viewHolder.myCheckBox.isChecked()) {
// Do something when checkbox is checked
} else {
// Do something when checkbox is unchecked
}
notifyDataSetChanged();
}
});
I ran into this exact issue! After Moonsoo's answer didn't really float my boat, I messed around a bit and found a solution that worked for me.
First, here's some of my code:
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final Event event = mDataset.get(position);
//
// .......
//
holder.mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
event.setActive(isChecked);
try {
notifyItemChanged(position);
} catch (Exception e) {
Log.e("onCheckChanged", e.getMessage());
}
}
});
You'll notice I'm specifically notifying the adapter for the position I'm changing, instead of the entire dataset like you're doing. That being said, although I can't guarantee this will work for you, I resolved the problem by wrapping my notifyItemChanged() call in a try/catch block. This simply caught the exception, but still allowed my adapter to register the state change and update the display!
Hope this helps someone!
EDIT: I'll admit, this probably is not the proper/mature way of handle the issue, but since it doesn't appear to be causing any problems by leaving the exception unhandled, I thought I'd share in case it was good enough for someone else.
Before notifyDataSetChanged() just check that with this method: recyclerView.IsComputingLayout()
Simple use Post:
new Handler().post(new Runnable() {
#Override
public void run() {
mAdapter.notifyItemChanged(mAdapter.getItemCount() - 1);
}
}
});
simply use isPressed() method of CompoundButton in onCheckedChanged(CompoundButton compoundButton, boolean isChecked)
e.g
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
... //your functionality
if(compoundButton.isPressed()){
notifyDataSetChanged();
}
} });
mostly it happen beacause notifydatasetchanged calling onCheckedchanged event of checkbox and in that event again there is notifydatasetchanged.
to solve it you can just check that checkbox is checked by programatically or user pressed it. there is method isPressed for it.
so wrap whole listner code inside isPressed method. and its done.
holder.mBinding.cbAnnual.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(compoundButton.isPressed()) {
//your code
notifyDataSetChanged();
}
});
I suffered with this problem for hour and this is how you can fix it.
But before you began there are some conditions to this solution.
MODEL CLASS
public class SelectUserModel {
private String userName;
private String UserId;
private Boolean isSelected;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserId() {
return UserId;
}
public void setUserId(String userId) {
UserId = userId;
}
public Boolean getSelected() {
return isSelected;
}
public void setSelected(Boolean selected) {
isSelected = selected;
}
}
CHECKBOX in ADAPTER CLASS
CheckBox cb;
ADAPTER CLASS CONSTRUCTOR & LIST OF MODEL
private List<SelectUserModel> userList;
public StudentListAdapter(List<SelectUserModel> userList) {
this.userList = userList;
for (int i = 0; i < this.userList.size(); i++) {
this.userList.get(i).setSelected(false);
}
}
ONBINDVIEW [Please use onclick in place of onCheckChange]
public void onBindViewHolder(#NonNull final StudentListAdapter.ViewHolder holder, int position) {
holder.cb.setChecked(user.getSelected());
holder.cb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int pos = (int) view.getTag();
Log.d(TAG, "onClick: " + pos);
for (int i = 0; i < userList.size(); i++) {
if (i == pos) {
userList.get(i).setSelected(true);
// an interface to listen to callbacks
clickListener.onStudentItemClicked(userList.get(i));
} else {
userList.get(i).setSelected(false);
}
}
notifyDataSetChanged();
}
});
}
This is happening because you're probably setting the 'listener' before you configure the value for that row, which makes the listener to get triggered when you 'configure the value' for the checkbox.
What you need to do is:
#Override
public void onBindViewHolder(YourAdapter.ViewHolder viewHolder, int position) {
viewHolder.mCheckBox.setOnCheckedChangeListener(null);
viewHolder.mCheckBox.setChecked(trueOrFalse);
viewHolder.setOnCheckedChangeListener(yourCheckedChangeListener);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.textStudentName.setText(getStudentList.get(position).getName());
holder.rbSelect.setChecked(getStudentList.get(position).isSelected());
holder.rbSelect.setTag(position); // This line is important.
holder.rbSelect.setOnClickListener(onStateChangedListener(holder.rbSelect, position));
}
#Override
public int getItemCount() {
return getStudentList.size();
}
private View.OnClickListener onStateChangedListener(final RadioButton checkBox, final int position) {
return new View.OnClickListener() {
#Override
public void onClick(View v) {
if (checkBox.isChecked()) {
for (int i = 0; i < getStudentList.size(); i++) {
getStudentList.get(i).setSelected(false);
}
getStudentList.get(position).setSelected(checkBox.isChecked());
notifyDataSetChanged();
} else {
}
}
};
}
I had the same problem using the Checkbox and the RadioButton. Replacing notifyDataSetChanged() with notifyItemChanged(position) worked. I added a Boolean field isChecked to the data model. Then I updated the Boolean value and in onCheckedChangedListener, I called notifyItemChanged(adapterPosition). This might not be the best way, but worked for me. The boolean value is used for checking whether the item is checked.
override fun bindItemViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
var item: ThingChannels = items[position]
rowActionBinding.xCbChannel.text = item.channelType?.primaryType
rowActionBinding.xTvRoomName.text = item.thingDetail?.room?.name
rowActionBinding.xCbChannel.isChecked = item.isSelected
rowActionBinding.xCbChannel.tag = position
rowActionBinding.xCbChannel.setOnClickListener {
setSelected(it.tag as Int)
if (onItemClickListener != null) {
onItemClickListener!!.onItemClick(position, null)
}
}
}
None of the past answers solve the problem!
The problem with past answers
All of them either avoid the problem by swallowing the change (i.e. not notifying the adapter of the change) if the user is scrolling, which just means that if the user was scrolling when the change was ready, they will never see the change. Or, they suggest using recylerView.post() which just postpones the problem.
The answer
Option #1
Stop the scrolling and then notify the adapter:
recyclerView.stopScroll()
val copy = workingList.toList()
//prevent IndexOutOfBoundsException: Inconsistency detected... (https://stackoverflow.com/questions/41054959/java-lang-indexoutofboundsexception-inconsistency-detected-invalid-view-holder)
workingList.clear()
recyclerViewAdapter?.notifyDataSetChanged()
//set display to correct data
workingList.addAll(copy)
recyclerViewAdapter?.notifyItemRangeInserted(0, workingList.size)
Option #2
For a better user experience, you can let them continue scrolling and listen for when they stop scrolling in order to update the UI, but this method should only be used if you do not plan on writing a function that will accept different RecyclerView instances, because if you add multiple listeners to the same RecyclerView which are all trying to be updated, the app will crash:
if(recyclerView.scrollState != RecyclerView.SCROLL_STATE_IDLE) //notify RecyclerView
else recyclerView.addOnScrollListener(object: RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(
recyclerView: RecyclerView,
newState: Int
) {
if(newState == RecyclerView.SCROLL_STATE_IDLE) {
//notify RecyclerView...
val copy = workingList.toList()
//prevent IndexOutOfBoundsException: Inconsistency detected... (https://stackoverflow.com/questions/41054959/java-lang-indexoutofboundsexception-inconsistency-detected-invalid-view-holder)
workingList.clear()
recyclerViewAdapter?.notifyDataSetChanged()
//set to display correct data
workingList.addAll(copy)
recyclerViewAdapter?.notifyItemRangeInserted(0, workingList.size)
}
})
Note: you can use option #2 even if you are writing a util function by keeping a list of previously registered instances and not add a listener if it has already been registered, but it is not "clean coding" to rely on state in a library/util class.
For me problem occurred when I exited from EditText by Done, Back, or outside input touch. This causes to update model with input text, then refresh recycler view via live data observing.
The Problem was that cursor/focus remain in EditText.
When I have deleted focus by using:
editText.clearFocus()
Notify data changed method of recycler view did stop throwing this error.
I think this is one of the possible reason/solutions to this problem. It is possible that this exception can be fixed in another way as it can be caused by totally different reason.

Android setOnCheckedChangeListener calls again when old view comes back

I cannot solve an issue with the getGroupView-method.
the problem is that the listener setOnCheckedChangeListener is getting invoked to many times.
Let say i check a certain checkbox-item. Then I scroll it out of view and then scroll back. What happends is that the listener is called once again. And the problem is that I store checkbox-id's in an arraylist inside this listener to use it later in the code. The consequence is that more elements is added to the arraylist everytime the listener is called and distortes the data.
Is there a solution to this? what should I do? Should I for instance unregister the listener?
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = null;
final int group_position = groupPosition;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.activity_phrase, parent, false);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.groupTitle);
viewHolder.checkBox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (buttonView.isChecked()) {
checked.add((Integer) viewHolder.checkBox.getTag());
}
else {
checked.remove((Integer) viewHolder.checkBox.getTag());
}
}
});
view.setTag(viewHolder);
viewHolder.checkBox.setTag(groupPosition);
} else {
view = convertView;
((ViewHolder)view.getTag()).checkBox.setTag(groupPosition);
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(titles[groupPosition]);
for (int i = 0; i < checked.size(); i++) {
if (checked.get(i) == group_position) {
holder.checkBox.setChecked(true);
}
else if (checked.get(i) != group_position) {
holder.checkBox.setChecked(false);
}
}
return view;
}
What version of Android are you using?
It seems to be an issue for multiple components, especially with a checkedChange() method (CheckBox, RadioButton) and I can't provide a good explanation why it is happening.
I would assume because it registers the change of the position state and grabs the change of other properties? A similar issue was addressed here
In any case, a workaround around this could be to add an OnClickListener().
CheckBox yourCheckBox = (CheckBox) findViewById (R.id.yourId);
yourCheckBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//is chkIos checked?
if (((CheckBox) v).isChecked()) {
//Case 1
}
else
//case 2
}
});
OnCheckedChangeListener worked for me with one if condition
if(buttonView.isShown())
{
//ToDo code
}
Add the below to ToggleButton layout:
android:saveEnabled="false"
This would make sure android doesn't store the check state to RestoreInstance and in turn won't cause the state change experienced by you.
Set the listener to null before calling setCheck() function, and enable it after that such as the following:
checkBox.setOnCheckedChangeListener (null);
checkBox.setChecked(true);
checkBox.setOnCheckedChangeListener (this);
Reference: Change Checkbox value without triggering onCheckChanged
Add an additional if statement isShown() in the OnCheckedChange method and it solves the problem that the OnCheckedChangeListener gets called multiple times.
There is no need to change to an onClickListener:
#Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isShown()) {
if (buttonView.isChecked()) {
//Do something
} else {
//Do something
}
}
}
strong textThere is many way to solved the issue
checkBoxSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// write here your code for example ...
if(isChecked){
// do somtheing when is checked
}else{
// do somthing when is removed the check**strong text**
}
}
});
**and there is another way **
checkBoxSelect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(((CheckBox)v).isChecked()){
//do something
}else{
//do something
}
});
I faced the same problem and struggled for several hours seeing all discussions related to this. I tried to fix this by keeping
viewHolder.checkBox.setTag(groupPosition);
this statement before viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() this listener. And the code works exactly as expected.
I was stuck with the same issue and found a way around by using on click listener
First check if the checkbox has the onclicklistener assigned to it or not. If not that means the adapter is setting the value for the first.
if(!holder.checkbox.hasOnClickListeners())
{
holder.checkbox.setChecked(data.get( position ).getSelected());
}
Now after checking this
holder.checkbox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
data.get( position ).setSelected(holder.checkbox.isChecked());
holder.checkbox.setChecked(data.get( position ).getSelected());
Toast.makeText(getApplicationContext(),
data.get( position ).get_number()+" : "+position, Toast.LENGTH_LONG).show();
}
});
Hopefully the scrolling issue goes away. Works fine here.

How to create single check box event for all check boxes in android?

I'm new to android it is possible in .net, But i want to build this logic in android is it possible to do that please help me, i know only creating check box event for each check box instead of that i want to create single event for all Check boxes please help me.
Vb.net
private sub Chkbox1_Checkchanged(byval sen as object,byval e as system.eventargs) handles Chkbox1.checked,chkbox2.checked,chkbox3.checked
blnvalue=true
end sub
android
chkbox1= (CheckBo1) findViewById(R.id.chkbox1);
rbtn1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
blnvalue=true;
}
});
implements onCheckChangedEvent for for all checkBox.
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.getId() == activity.getCheckRefresh()) {
activity.setRefreshRate("1");
activity.setEnable(isChecked);
return;
}
}
Assuming you are in an Activity, you can make your Activity implement View.OnClickListener, and then use this for each View within your Activity, even ones that aren't CheckBox.
public class MyActivity extends Activity implements View.OnClickListener {
//declare checkboxes
public void onCreate(Bundle something) {
//setup stuff and get views
rbtn1.setOnClickListner(this);
rbtn2.setOnClickListner(this);
rbtn3.setOnClickListner(this);
rbtn4.setOnClickListner(this);
}
public void onClick(View v) {
if(v == rbtn1) {
//do stuff for checkbox1
} else if(v == rbtn2) {
//do stuff for checkbox2
} else if(v == rbtn3) {
//do stuff for checkbox3
} else if(v == rbtn4) {
//do stuff for checkbox4
}
}
}
You can also use onChackChanged Event & OnClickEvent for checkbox.
chkbox1= (CheckBo1) findViewById(R.id.chkbox1);
chkbox2= (CheckBo1) findViewById(R.id.chkbox2);
chkbox1.setOnClickListener(this);
chkbox1.setOnCheckedChangeListener(this);
chkbox2.setOnClickListener(this);
chkbox2.setOnCheckedChangeListener(this);
public void onClick(View v)
{
if(v.getId() == R.id.chkbox2)
// your code
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.getId() == activity.getCheckRefresh()) {
activity.setRefreshRate("1");
activity.setEnable(isChecked);
return;
}
}
u can do this.. here click is an inner class.. and create an instance of it n pass it to every setOnClickListener(click)
class click implements OnClickListener
{
public void onClick(View v)
{
// your code
}
}

Categories

Resources