Android ListView with CheckBox and a selectable item - android

I have seen plenty of samples which support a checkbox for each item in a list but this seems to be about as far as they go. My problem is that I have two lists of items which interact.
The first list displays Method Statements - when the user select a method statement the second list displays a list of Risk Assesments for this method statement. I have this all working with no issues.
I now need to extend this so that the user can select (via a checkbox) the Method Statements that they want to use. What this means is that I do not want the selection of the listitem to set or clear the check box, just populate the second listbox. I do however need the user to be able to manually check the item to include it.
Any suggestions please would be most welcome - maybe a ListView is not the correct way to do this, perhaps a dynamically created table within a scroll view?

I would try this way.
Set choice mode of ListView as NONE.
Build a ViewGroup (Linear/Relative/Frame Layout) with TextView and CheckBox. It will
be a cell of ListView.
When you bind view in the adapter, set CompoundButton.OnCheckedChangeListener on the CheckBox. Place position of your view in the Integer and set it as a tag for your cell (remember: do it in the binding data with view).
private static class MyOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
#Override
public void onCheckedChange(...) {
Integer position = (Integer) view.getTag();
//your logic with position
}
}
Or just set OnClickListener.
The idea is: let CheckBox be clickable and let it consume click events - then its parent will not receive this events and AdapterView.OnItemClickListener will not be fired.

Related

Android: ListView with Checkbox

I've searched high and low for this issue but am not able to find something concrete.
Basically, I have a list of friends in a ListView and I am trying to create a group by choosing specific friends. To do this I have created an Activity with a SearchView and a ListView. Searching will filter and display friends that I want to add. To do so, I want to use a checkbox. All the "checked" friends will then be passed to another activity where the group creation can be complete.
The activity's layout has a ListView.
I have written my own adapter and this adapter inflates the row layout for the ListView. The layout of the adapter has a CheckBox that I have explicitly added.
So activity layout has: ListView
Row layout has: TextView and Checkbox
But am having issues with:
1. Trying to keep the checkbox checked when scrolling/searching the listview as the view refreshes and the checkbox is recreated as unchecked - I've solved this for the time being by maintaining a list of the checkboxes clicked. I am doing this in the getView() method of the adapter.
2. Trying to determine which row of the ListView the checkbox belongs to. I'd need this to then create my group of friends.
3. Allowing to edit the list of chosen friends - to add/remove friends before creating the group.
Here is a diagrammatic representation:
EDIT
Adding in another diagram to show how it works.
So from the first activity, I go to the second one to search and add friends. Once I've chosen my friends, I send the list back to my first activity when I click on 'OK' button.
I finally figured this out.
As mentioned, I was not sure how to pass the "control" back to the activity when the checkbox is checked. This was an issue because the checkbox was part of the layout inflated by the adapter.
Within the adapter, I set this up to pass the "control" back to the activity to handle the check on the checkbox:
checkbox.setOnCheckedChangeListener((SearchFriendActivity) context);
And within the SearchFriendActivity, the check on the check box can be handle by:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i(TAG, "Inside onCheckedChanged");
//The below code line will get the position of the row item
int pos = friendlistview.getPositionForView(buttonView);
if(pos != ListView.INVALID_POSITION)
{
//Logic Here
}
}
Remember to implements CompoundButton.OnCheckedChangeListener in the activity.
A neat little tutorial on this
here
There is AlertDialog with method setMultiChoiceItems() You can make dialog with checkbox list. Could it help?
I don't know it work or not because i'm not on my dev pc right now i can't post you the code. But try this:
1 Implement checkbox event listener (I'm not sure maybe onCheckChanged()) and store the checkbox value or index in list (better use index)
2 if u had store index, u can tell which checkbox belong to which row. if not maybe u should setTag() to row or TextView of row so u can compare it before intent to next activity.
3 you don't tell us what view that group use. It hard to answer but i think value u got from getIntent() maybe a list so u can display it with some view with remove button. So just remove value from list before create group.
For add more friends maybe u should intent the list intent back to first activity and check the checkbox where it checked and allow user to check/uncheck it again. After that just intent it to that group activity again.
This is an easy task, follow the steps:
make a layout xml file and define how a single check box will look
create a list view.
populate the listview as listview is populated for simple textview.
If you don't have enough idea about populating list view then try this link
http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html
let me know if you face any problem with this.
happy coding!!

Manipulating a group view in a ExpandableListView from the child view

I'm building an ExpandableListView in which there are groups of radio buttons.
Currently, I am able to handle the click event on each radio button and select the corresponding one and deselect the others. But there is just one more thing needed to make it perfect. That is displaying the value of the selected checkbox in the group view so the user can see what is selected without expanding it.
For that purpose, I have a TextView in the group view. Is it possible to update that TextView when a checkbox is selected in the child view?
Here is a screenshot of what I have right now:
Basically what I want to accomplish is to make it write the value of the selected checkbox instead of "Regular".
Thanks.
in your example, it seems perfectly reasonable to save a reference to your TextView inside a member variable when it is created. you can then easily update its value by calling setText() on it from within your checkbox's click handler.
it is neither necessary nor desirable to call notifyDataSetChanged().

How to get correct position of a checkbox in a scrollable listView?

I have a scrollable listview that has 2 textViews, 3 imageButtons and 1 checkBox in each row.
In the Header of the listView, I have a delete button just above the column containing all the checkboxes of the listView.
I have to perform the delete operation of objects in the listView when 2 or more checkBoxes are checked randomly by scrolling the listView and thereafter the delete button at the top is clicked.
But the problem is that i am not getting the correct poition of the checkbox that was selected. Moreover, sometimes i get the correct position but still the object to be deleted passed is wrong. Hence the entire functionality is affected maybe due to the scrolling nature of the list.
Should i take the position in the holder of the adapter class and also bind the state of the checked or unchecked checkbox with my object.
And should I use checkBox.setOnCheckedChangeListener() or deleteButton.setOnClickListener().
If i use the latter one, then how to get all the corresponding objects of the list whose checkboxes were checked before pressing the delete button?
And where should all the related code be placed..in the listAdapter class or in the activity?
Please help me find a solution to this problem..
first of all in your getView() method you should set a specific Tag to each checkBox. For example: check1.setTag(position) then you should implement both OnCheckBoxChangeListener for your checkBoxes and OnClickListener for your delete button. As you know setting of onCheckBoxChangeListner have to be in getView() method. Then you add positions of list that their checkBox is checked to the a ArrayList with the help of getTag() method of chechBoxes in onCheckedChanged() method.
Try getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {}

how to set multiple text views with radio button in list view single choice mode in android using simple adapter

I have problem in list view with single choice mode. I want to display three text views with one radio button in list view. list view working properly. The problem is single choice mode. i want to select only one list item at a time rest of thing unselect mode. I searched last three days still i won't get any idea. Could you please help me. Thanks in advance.
You could make a variable that saves the radiobutton chosen.
In each radiobuttons onclick you could set the variable value and set the rest of the radiobuttons to unchecked.
Hints:
in the radiobutton xml add: android:onClick"method name"
In the method add View view as an argument (example: public void clickMethod(View view).....)
Im not sure if you can display 3 textviews and a radioButton without customize ArrayAdapter
but if you have succeeded to build the list you can do the following
1- remove the focus from the child elements(radioButton and textviews)
you can do this from the xml [android:focusable="false"] and for the listview [listview.setItemsCanFocus(false)]
2- make the radioButton not clickable from xml[android:clickable="false"]
3- create a variable to hold the clicked item (in the custom adapter) and make public method to update this position
4- create OnItemClickListener for the listview, update the clicked position in the adapter
and call adapter.notifydatasetchanged
5- be sure to in getView method to make the radioButton unchecked and make the view at that position is checked

Checkboxes in a ListView - When I select the first, it checks the last and vice versa

I've set up a ListView that contains a Checkbox in each row (along with a TextView). For some reason, when I "check" one of the boxes, it seems to "check" whichever box is opposite in the ListView (i.e. when selecting the top box, the bottom checkbox becomes selected while the top remains unchecked.
I realize there isn't any code here to work with, but I was just wondering if this is a general problem? If not, I can try to post some code. Thanks!
Views are recycled in a ListView. That's why some are checked when you think shouldn't be.
Here's the deal: The checkbox has no idea which item in your adapter it represents. It's just a checkbox in a row in a ListView. You need to do something to "teach" the rows which item in your data set they are currently displaying. So, instead of using something as simple as a String array as the data for your adapter, create a new model object that stores the state of the checkbox in it. Then, before you return the row in getView(), you can do something like:
//somewhere in your class
private RowData getData(int position) {
return(((MyAdapter)getListAdapter()).getItem(position));
}
//..then in your adapter in getView()
RowData object = getModel(position);
if(object.isChecked()) {
myCheckbox.setChecked(true);
} else {
myCheckbox.setChecked(false);
}
//then retun your view.

Categories

Resources