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
Related
I have a customized list-view and each row of the list-view contains a check box and a text field. Clicking on any check-box in any row of the list-view should disable other check-boxes.How to achieve this.
First of all you should use recycler view, it is way better and practical than listview.
To-do that you need to have a custom adapter class which is extended from BaseAdapter.
You need have a List list for your adapter to iterate over. And in your custommodel you need to add new field called private boolean selected'.
Then you need to define onclick listeners for the checkbox in the getview() method of the adapter.
Then in the onclicklistener.onclick method you should get the position of the clicked item. And iterate over all your data and set 'item.selected = false;'
and only set list[clickPosition].selected = true;
Then call notifyDataSetChanged();
I hope it is clear for you, if not post some more data.
First I'd suggest using a RecyclerView, I'll answer the question for a ListView, but really either way the solutions will be similar.
Assuming you're using the ViewHolder pattern in your BaseAdapter. Inside your getView function set a onCheckedChangedListener and inside that create a function that sets an isListChecked boolean inside the class that implements your adapter's Interface for setListChecked, should look something like:
cbListItem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
myInterface.setListChecked(isChecked);
notifyDatasetChanged();
}
}
);
Inside getView you will also need to check the isListChecked boolean and set that as the CheckBox state
Inside your custom adapter in getView method :
if(your condition==true){
checkBox.setEnabled(false);
} else {
checkBox.setEnabled(true);
}
declare an array list of checkbox.
Inside your custom adapter in getView method :
put checkbox inside an arraylist.
if(your condition==true) {
for(CheckBox c: lsitOfCheckBox){
c.setEnabled(false);
}
}
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
Could someone please explain the different ways of making checkbox list and saving the checked option? It would be nice if you could attach examples. I am using arraylist to save input, what are the other ways?
final CharSequence[] items = {};
final ArrayList seletedItems = new ArrayList();
Define your ListItem object to have a 'checked' field
class ListItem{
boolean isChecked=false;
}
In your list adapter's getView attach a onCheckedChangeListener to the CheckBox and change the checked state of your object. something like:
final MessageItem Message=getItem(position);
message.setText(Message.text);
//set data
select.setOnCheckedChangeListener(null); //important so that when reusing the view the old listener isn't called
select.setChecked(Message.selected);
select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
Message.selected=isChecked;
}
});
You can create a custom class for objects like,
public class entity
{
public boolean isChecked;
//Any other variables can also be created
}
On check box selection change the value of
entityobj[index].isChecked=true/false;
Apply a custom list adapter,
in adapters getview() method you can check for isChecked value and then either check or uncheck the respective checkbox,
using this your selection will persist while scrolling also..
Hope it will help...
I have an activity that has a checkbox, then under it is a list of contacts populating my listview with checkboxes for each contact. I have two major problems stemming from my question.
chkbox_foo is outside of the listview, and chk_bar is inside. chk_foo works, but anything related to chk_bar after being initialized causes the app to crash. Also, if I create a setOnCheckedChangeListener for chkbox_bar, that will cause the app to crash also. Does anyone know why this is happening and how I can fix this?
btn_foo = (Button) findViewById(R.id.btn_foo);
barList = (ListView) findViewById(R.id.lv_barList);
chk_foo = (CheckBox) findViewById(R.id.cb_foo);
chk_bar = (CheckBox) findViewById(R.id.cb_bar);
// set checkboxes state as false at beginning
chkboxAllVisible = false;
chkboxSingleChk = false;
chk_foo.setChecked(chkboxAllVisible);
chk_bar.setChecked(chkboxChk); <---App crashes here
// Outside of listview checkbox
chk_foo.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Log.d(TAG, "checkbox changed " + isChecked);
if(isChecked){
chkboxAllVisible = true;
chk_bar.setChecked(isChecked); <---app crashes here too
}
}
});
// Outside of listview checkbox
chk_bar.setOnCheckedChangeListen... <---app crashes here also
When the user clicks on your "master" checkbox you will need to iterate through the list that is bound to your ListView. Then by accessing each row individually can you mark each row's checkbox.
Please post the code for your ListView if you want a specific example.
chk_bar is probably null. You can't call findViewByID on the main layout if the checkbox is inside a listview. You have to call it on the layout where it is housed/contained, which would be the listview item.
That said, you don't need to do that. If you have contacts with checkboxes, I'm guessing you are using some sort of custom adapter? If so, good, then it this will be easy. I'm also assuming you are keeping track of whether or not an item/contact has been checked (if not, you'll need to implement that anyways, see tutorial below)
In your getView of your custom adapter, while generating the view, you need to look at the data object and if the IsChecked value is true, then you check the checkbox.
public View getView(int position, View convertView, ViewGroup parent)
{
//Inflate your view
//View listViewLayout = ...
//Here is where you get the reference to the checkbox
Checkbox chkBox = (CheckBox)listViewLayout.findViewById(R.id.cb_bar);
//Get the data object from whatever your source is (maybe an array list?)
SomeObj curObj = (SomeObj)listOfItems.get(position);
chkBox.setChecked(curObj.IsChecked);
}
Here is a full tutorial on how to implement a custom Adapter for Listview:
http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter
Once you have that working, the rest is simple:
When the user clicks the master checkbox:
Regenerate the list of items but force all the checkbox values to true.
Call adapter.notifyDataSetChanged()
When you call notifyDataSetChanged() the listview will re-draw itself (if it believes the source items have in fact been changed. See this: notifyDataSetChanged example)
When it regenerates the listview, it will iterate through your source items you passed in, and since you regenerated the list and marked all items as "IsChecked" (or whatever your variable name is), it will check each checkbox.