Android Search View with recycler view and checkbox in recycler items - android

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.

Related

Operations on ListView with an adapter

I have an Activity with a ListView and an adapter. Each row in my ListView contains one EditText and one CheckBox. I have two problems with this ListView:
first: what is the best way to save checked checkboxes after changing device orientation or pressing home button?
second: I implemented functionality to delete selected rows. When I click on checkbox and press a "delete button" the I call notifyDataSetChanged on my Adapter and everything is ok but if selected row isn't last row on my list then after all operations first field after deleted field becomes checked. Why?
The listview is recycling the views all the time. That means that the checkbox in the field after the deleted field is the SAME checkbox as was in the deleted field. That is why it becomes checked.
The solution is for the checked state to always be stored outside the view, in the activity. The easiest way is probably a Map of some key to a Boolean.
Try this..
Create Model fro list item and store checked and unchecked value to one variable and also you have to use getTag() and setTag() method.

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!!

Android - Get item from listview with custom adapter through checkbox

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)

Android Custom list view with checkbox

i have list view with checkbox and if I select some item than every 9 item is selected too.
I am using onClickListener on checkbox.
Do you know how solve the situation? One item checked, but more selected.
If you wanna see my source code, just let me know and I will put there.
Thanks for help
Lukas P.
Your problem comes from views being recycled by Android. You need to store the check box state of each row in some container (eg. a list) and then explicitly set the checked state to the correct state in getView.
See this similar question:
custom ListView with checkboxes checking unchecked items

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() {}

Categories

Resources