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!!
Related
I have an activity which has, search view and recycler view. The recycler items contains checkbox, Assume there are 5 items(A,B,C,D,E) in recycler view . In normal, when I click checkbox on A/B/C/D/E,it works fine, when I search C on search view, the filter is done and it shows result of C alone, when I check C and close the search, the check is reassigned to A as it is in position 0 and C remains unchecked. I need to check based on user Id. How to achieve this?
For that purpose you need to validate data in proper manner.You just store the id of checkbox item when user is selecting to it.and after that you validate the checkbox data in onBindView() method of recycle adapter. I hope its help you.
For questions without code references, one can expect answers in the same manner, so first, try putting some code as well in the question.
In a nutshell, you need to save the selected state of the checkbox in your data model so that when the list is reloaded, you can get that selected state of checkbox and assign it to isChecked property, this can be done in onBindViewHolder. Hope this helps.
I have a listview with a custom adapter (extends BaseAdapter).
It recieves a list of objects I have to populate a ListView.
One of my object's atribute is a boolean called "checked".
on my method getView, this atribute is responsible for checking or not checking a CheckBox on my View.
Everything is working just fine and when my Activity loads, the ListView itens apear as they are on my list of objects (which was received from my database), some checked and some not checked.
But when I check one of my ListView's checkbox, I need to update my object and therefore it's value on my database. The problem is:
"How do I know which item (object) I have to update just by checking my CheckBox?"
"Don't they have the same name?"
I have a listView.setOnItemClickListener(...) where I can get my object by it's position, but it works when I click on the "row" of my list view itself, not on my checkbox... I thought about using it to check/uncheck my CheckBox... But how would I do that? Can I use the position to get a specific CheckBox from my listView?
In the end, I also thought that the best method would use the "listView.setOnItemClickListener(...)" to check my CheckBox, once it would be easier for my user to check one Item by it's row than by a tiny CheckBox, so can someone help me with the best way to solve my problem?
I'm sorry I didn't post my code, but right now I can't access it.
Take a look at this tutorial
You need to add an method in the onClick of the checkbox that will be implemented by the activity. One way to do that is to add an abstract method to the adpater and make the activity implement it. lets call onCheckBoxClicked(int position)
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() {}
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
I have a ListActivity binding to a database with a Custom SimpleCursorAdapter.
in each of my list items I have 4 texts, image and checkbox (each one is set to focusable:false).
In the list itself I have a long button so when I press on it, I want it to delete all the
rows which thire checkboxes are 'checked'.
Now, I have tried many techniques in order to achieve a simple operation (click listeners, CheckedtextView as shown in the tutorial) but was not successful.
There is also another weird phenomenon occurring, after I #Override onListItemClick, also i dont get any calls at all while I am pressing on any of the list's rows.
Does anybody have any idea how to solve these issues? Thanks.
Thanks,
Moshic.
Are you calling super.onListItemClick(ListView, View, int, long);? if so, try write out a Log.d("ListView", "My list has been clicked"); and see if that comes out in the Logcat. If it doesn't, try extends ListActivity in your activity declaration. comment back if you want my help, I'll be ready to help as much as possible
create your own adapter with following features
-in getView() you have to set click listener for check box(it will be own listener for each check box)
-each element of adapter contains flag checked/unchecked, and you should set it by check box click listener from previous point
in activity with delete button click listener you have to get checked elements from adapter and do with them what you want
after then you should update list view for example by calling (your adapter).notifyDataSetChanged()