I have a ListView that is populated using a custom CursorAdapter. Within BindView, I have the following code:
CheckBox mCheckBox = (CheckBox) view.findViewById(R.id.list_done);
mCheckBox.setChecked(isDone);
mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
AW.getDB().updateTask(c.getInt(c.getColumnIndex(ToDoDBAdapter.KEY_ID)), isChecked);
TD.displayTasks();
}
});
However, when one of the checkboxes in the list is set, it seems as though the code that runs applies to the cursor's position as of the last item in the list. Thus, in a list of 4 items where
c.getInt(c.getColumnIndex(ToDoDBAdapter.KEY_ID))
should return 1, 2, 3, 4 respectively, checking either of the 4 boxes gives the value 4. Anyone know why this is?
In other words, the onCheckedChangedListener is the same for every CheckBox in the list.
Related
I have a custom list view and each item of it has a check box and a text field. I want to disable the check box in every other row of the list view if the check box of the first row is enabled.
Since you are stating that if the first check box is clicked all the others should be disabled, do a check in your onBindView() to see if the checkbox is checked. if it is, set a value to say that it is, and for all the following list view entries set the checkbox to disabled.
In your onCheckChangedListener():
checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked()){
firstCheckboxChecked = true;
}
});
Then in your onBindView for the rest:
if(firstCheckBoxChecked){
checkbox.setEnabled(false);
}
Here I am putting some rough solution which will help you.
classs model
{
boolean status;
model(boolean status){
this.status=status;
}
}
List<model> list_model= new ArrayList<>();
list_model.add(false);
list_model.add(false);
list_model.add(false);
list_model.add(false);
add this model with listview adapter.
//Adapter coding
checkbox.checked(check)
//write below code in checkbox.setOncheckedItemSelect method.
//you will get already checked parameter in that method.which will put into list
list_model.add(checked);
//here call listview notify datasetchangedListener.
create a boolean variable isFirstEnbled in your adapter. then on click first item's checkbox set this value as isFirstEnbled=true .
adapter.notifyDataSetChanged()
This recreate the view with isFirstEnabled set as true
I've placed a checkbox in a custom array adapter of an listview. The adapter works fine when I check the checkbox. But when I try to uncheck the checkbox in a random manner the application throws.
Here's my code for your reference:
holder1.chckbx1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (isChecked) {
Log.i("checked", "checked");
positionArray.set(position, true);
broadCastDetailsLists.add(new BroadCastDetailsList(cpsId2, cpsType2, position, cpsName,handShakeStatus, favouriteStatus));
sendEnquiry.add(new Q2_SendEnquiryList(searchresultList.get(position).getCpsId(), searchresultList.get(position).getCpsName()));
} else {
Log.i("unchecked", "unchecked");
positionArray.set(position, false);
broadCastDetailsLists.remove(position);
sendEnquiry.remove(position);
}
}
});
Also here's my log for your reference
It's an issue with your logic. When checking a checkbox you'll just add data to lists, while removing an index when unchecking.
Say you check the item at position 1. broadCastDetailsLists and sendEnquiry now have a size of 1. If you uncheck the same checkbox, you remove position 1, thus the IndexOutOfBoundsException.
Make Sure your ArrayList data not found or null , print the list data first and check data .
cbChecked.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
FaxDialogActivity.record.get(position).isChecked = true;
}
});
but when i check the one checkbox, multiple checkboxes get selected after scrolling the listview.
This is because getview is called everytime the view recycles while scrolling a list view.So u have store a value and check it in getview to get checked checkbox.
Try this
1> Create an ArrayList of Boolean Object to store the state of the each CheckBox
2> Initializes the ArrayList items to default value false, means no CheckBox is checked yet.
3>When you click on CheckBox. Set a check against Checked/Unchecked state and store that value in ArrayList.
Now set that position to CheckBox using setChecked() method.
for complete code please refer THIS ANSWER and this BLOG
Here's my aproach:
I have a custom ListView containing a custom Adapter containing two different kind of Views. One of them has a CheckBox in each View.
I just want to notify the Activity when one of those CheckBoxes have been clicked, and to pass it a boolean: true if ANY of the boxes are checked, false otherwise.
How should I do it?
I just need the theorethical answer, not code.
Thank you very much.
1- implement the activity by checkedChangeListene and override the code in activity
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
Object obj = buttonView.getTag();
// perform logic
}
}
2- pass the activity in custom adapter constructor.
3- set in getView
CheckBox chkBx = (CheckBox ) findViewById( R.id.repeat_checkbox );
if(null!=chkBx ){
chkBx.setOnCheckedChangeListener(mActivty);
chkBx.setTag(position);
}
I am trying to make a gmail-like listview. That is, initially, the listview is in single choice mode, but when a checkbok is selected, it becomes multiple choice.
this is what i have so far:
SimpleCursorAdapter dataSource = new SimpleCursorAdapter(this, R.layout.listitem, cursor,
new String[]{"_id","a","b","c","d","e","f","g","h"},
new int[] { R.id.checkBox1, R.id.a, R.id.b, R.id.c, R.id.d, R.id.e}
// all of these are a part of my list item custom layout.
);
dataSource.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
#Override
public boolean setViewValue(View aView, final Cursor c, int i) {
String s=c.getString(i);
String toset="";
TextView tv = i==0?null: (TextView) aView;
if (i==0){
if(chkL==null){
chkL=new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton cb1, boolean isChecked) {
CheckBox cb=(CheckBox) cb1;
chkid[c.getPosition()]=isChecked;
if(lv.getChoiceMode()!=lv.CHOICE_MODE_MULTIPLE){
lv.setChoiceMode(lv.CHOICE_MODE_MULTIPLE);
}
lv.setItemChecked(c.getPosition()+1,isChecked);
log(isChecked+" selected "+(c.getPosition()+1));
}
};
}
CheckBox cb= (CheckBox) aView;
cb.setOnCheckedChangeListener(chkL);
return (i==0);
}};
Currently I am trying to at least make the item "selected" by pressing the checkbox against it. I am using c.getPosition()+1 (cursor's position) to get the position of the list item tapped. but that always seems to return 1 or sometimes, random values.
update:
Looks like it always returns 1, but when i iterate through getCheckedItemPositions(), i see that item no. 10 gets selected. Tap another checkbox, again the cursor position will be 1 but item no 11 will get selected and so on. But if i tap outside the checkbox, the correct item is selected.
Also, on selecting one checkbox, if you scroll down, you would see that several other checkboxes are checked. (I have about 255 items in the list). I think its due to android's way of reusing the same checkboxes ?
I would try a slightly different approach:
Keep the ListView as SINGLE_CHOICE, but set each row's CheckBox with a listener that adds / removes the row's id to a List called checkedRows (for example).
When you want to delete, move, star, etc the checked rows: iterate through checkedRows and perform the necessary action.
I believe that is simpler than changing switching back and forth between multiple types of the default states and functionality.
Addition
I have never overridden setViewValue(), but this should work:
CheckBox cb= (CheckBox) aView;
cb.setOnCheckedChangeListener(chkL);
cb.setTag(c.getLong(c.getColumnIndex("_id"))); // Add this
and in your onClickListener():
long id = (Long) cb1.getTag();