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...
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 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);
}
}
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
Iv'e got a custom Listview implemented in this particular class, it looks like
[Image/Textview/Checkbox]
When someone clicks on the text is should launch another class, this is working at the moment, however when someone clicks a checkbox I want to update an array that stores the current state of all the checkboxes, I tried doing that via using a onCheckedChanged method within the custom adapter however it doesn't work as the position variable isn't final. So two questions
1 within the adapter is their a way to get the position of the Checkbox so I can change the right position in the array, and
2 If not is their another way to update the array somewhere else without disrupting the current functionality of the onClick.
Here is my onCheckedChanged within my adapter
CheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton but, boolean b) {
// TODO Auto-generated method stub
if(but.isChecked())
{
deleteList[???] = true;
}else{
deleteList[???] = false;
}
}
});
Have a look a this method. You can set a tag to a view containing any object you wish, which in your case may be the integer value associated with the CheckBox's row.
So when you create your CheckBox:
but.setTag(new Integer(position));
This would change your if statement to:
if (but.isChecked()){
deleteList[((Integer) but.getTag()).intValue()] = true;
} else {
deleteList[((Integer) but.getTag()).intValue()] = false;
}
Let me know if this helps!
I currently have a ListView in my Android app that has a CheckBox on the side.
When the checkbox is changed I want to get some info from the listview row that the button is in and then persist it, however, I can not work out how to get the row data from the onClickChanged listener.
My listview is populated using a custom JSONObject listadapter, I am hoping it is something similar to working out what row is selected when detecting the onLongTouch - for example below is the code I use to get the relevant JSON object for the row selected on a longTouch:
public boolean onContextItemSelected(MenuItem item) {
//get row selected information
AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
JSONObject selected = (JSONObject) getListAdapter().getItem(menuInfo.position);
The above is obviously within my ListActivity, and my onCheckChanged listener would not be in the same activity (as it is defined in the row rather than the list)
Any help would be appreciated!
One possible way to solve this is to attach some info to the checkbox when binding occurs.
In your Adapter, add (or modify) the bindView override like this to add a tag thatcontains the context information you need:
#Override
public void bindView( View view, Context context, Cursor cursor){
CheckBox checker = (CheckBox)view.findViewById(R.id.checkbox);
checker.setOnCheckedChangeListener(MyActivity.this);
checker.setTag(Long.valueOf(cursor.getLong(cursor.getColumnIndex(BaseColumns._ID))));
// call super class for default binding
super.bindView(view,context,cursor);
}
(or attach any other value to the checkbox that helps you identify the item, e.g. the propery name or index)
Then, in the onCheckedChanged, you can refer to this information to identify the item that was changed:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Long id = (Long) buttonView.getTag();
if ( id!=null)
Log.d(TAG, "onCheckedChanged id="+id);
}