So I have a to-do list, just the typical checkbox, and textview recycler view list.
I want recycler view to remember the check state of all the checkbox in the list and when click Submit button, the list of checkstate of all checkbox in the list will be updated to a room database, but I've not been able to find a way.
These are some of the points I have found about the problem:
recycler view should not reference database (dao- viewmodel_
you can update in the onbindviewholder in adapter (though I don't understand how)
Thanks you.
This is a potential pseudo guideline of doing this:
You need a model/pojo class that has a field to maintain the checkbox state, add the getters, & setters for it.
A list of this model class is passed from the activity/fragment to the RecyclerView adapter.
onBindViewHolder() should update the checkBox of each row from the list.
Whenever a checkbox is checked, you should update that on the corresponding list position, and a listener (which is implemented by the activity and passed to the adapter to trigger its callback whenever you update the list item (like changing the checkbox value).
When you click the save button, save the list in Room.
Whenever you show the activity, it should get the list from Room and pass it to the adapter.
Related
I have a RecyclerView.Adapter setted in a RecycleView which belongs to an Activity. This Activity contains a button. This adapter contains rows which have checkboxes. I would like to hide checkboxes of all rows when this button is clicked.
I'm using ViewHolder's pattern in my adapter.
I'm thinking in this solution:
Pass in my activity as a parameter to adapter's constructor. This activity contains a method to verify if button was clicked. And when this button is clicked the value changes and calls notifyDataSetChanged(). So the adapter shows/hides checkboxes.
Based on a MVP approach is this a good idea?
Possible solutions:
Each view holder is related to a model class. in this model class, you can hold a boolean that indicates if the corresponding view holder should show the checkbox.
When the button is clicked you can iterate over the items that should be affected and change the boolean to true. Then notify data changed on the affected items.
The adapter can hold a boolean that indicates if all the checkboxes should be hidden.
Each view holder will receive the adapter as a reference upon creation (and not the activity - which is bad) and will check this boolean whether to show or hide the checkbox.
The adapter boolean will be updated when the button is clicked and then will trigger a notify data changed.
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.
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 list view in an android program. Each row of this list view has a button. When user clicks one of these buttons, an action must be performed on data that is shown by that row.
How can I get access to that data and manipulate it?
Call getItem() on your Adapter to retrieve whatever the data is that is at that position within the Adapter. So, for example, if your Adapter is an ArrayAdapter<Restaurant>, getItem() will return the Restaurant for a given 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() {}