Hide checkboxes when button is clicked - android

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.

Related

How to update check recyclerview list in room database?

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.

How to update layout of item view in listview on Activity

My app have an button(B1) custom ListView that item view include : checkbox,textview
Goal is I press button B1 then all checkbox in ListView will be setVisible and I had done it BUT THIS IS WORK FOR WHAT IS SHOWING ON SCREEN. When I scroll Listview in case multi-rows that the others not be visibled
Let's see my explanation:
- Example that there is 10 items in list data -> Listview on current screen just show 5 items ( 10 items is still visible when scroll)
- When press button B1 that just CHECKBOX of 5 items is visible, the others is not working
I mean I want to change layout of view item right on Activity
Because you are only updating the views of layout. You need to also update your data set (array of your adapter of listview) but I know you don't have a boolean variable in your data set to decide whether checkboxs are visible or not. First you need to add variables. After button clicked, don't update views of layout. Only change the datas (boolean variables) and call notifyDataSetChanged() method of your custom adapter. notifyDataSetChanged() method will recall your getView() method of your adapter for all views visible in your screen. Values will be updated correctly if you check visibility of views in your getView() method. Other items after scrolling will be correct because your getView() method is correct.
If you don't understand the answer add your OnClickListener of button and your custom adapter class. I will update them.
Edit: You have focused on the wrong problem. Your solution isn't related to updating the layout. You have problem about logic of how listView should works.

How to click button inside Activity to change CustomeView item attributes

I have an activity which contains one button and one RecyclerView. The RecyclerView is done by the standard way - AdapterView and ViewHolder(inner class for the Adapter).
Inside the ViewHolder I have an image and text on CardView.
I want to show/hide the image inside the CardView,inside the ViewHolder by clicking the button in my Activity Class.
However I cannot figure it how - I cannot instantiate ViewHolder are access it as static.
When I instantiate the Adapter and call notifyItemChange() it also does not work.
Any help on how to do it?
You can use two different approaches:
GreenRobot EventBus or any other even bus. Dispatch an event from the button click; and any card cell will listen and show/hide depending of the event
every cell view holder would need a model (similar to array of items for a ListView). This model may have a boolean showImage(true/false)
when button is clicked, iterate through the array of models calling Model.showImage(true)
after updating every mode, call Adapter.notifyDataSetChanged()
in [Recycler.onBindViewHolder()](http://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#bindViewHolder(VH, int)) get a reference for the model of that position and show/hide the image based on the model boolean behind Model.showImage()

How to access views within ListView rows?

I have a ListView that is set up with an ArrayAdapter. The adapter gives each row of the ListView a CheckBox with some text. The adapter is based on Strings and basically just creates a CheckBox and sets its text to the corresponding String. Outside of the ListView there is a Switch that when turned on should check all the CheckBoxes in the ListView. I am having difficulty being able to access the CheckBoxes within each row of the ListView in the method that handles the Switch being clicked. I tried using getItemAtPosition(), but as far as I can tell it only gives me the String that is the CheckBox's text. Can someone please provide some help on how to do this?
To get the view for list item use getChildAt() on the list and then you can call findViewById() on the view to get the checkbox, however this will only work for views that are visible in the list.
To make your life easier create a custom array adapter, with a custom class. The class should have a boolean field like isChecked and in getView() set the checkbox to checked or unchecked depending the value of the isChecked field, then you can use getItem() to retrieve the class of list item you want to change the value of isChecked and call notifyDataSetChanged() on the adapter.

Overriding Android ListView onItemClick causing item to be selected (checked)

I am trying to better understand the internal functioning of ListView as it pertains to selecting one or multiple items- it's actually amazing how difficult it was for me to even get this far in my understanding.
By default a regular click on a ListView item is setting the 'checked' state for that item to true. How do I override this behavior so this selection does not happen?
And more fundamentally, what are the underlying ListView mechanics here? Is the row view's default onClick then calling the ListView's onItemClick/LongClick handlers, or how does this click handling get sequenced?
I do want to allow a choiceMode of multipleChoice, but I only want to select it onItemLongClick. Overriding onItemClick does not change this behavior, and overriding the row view's onClick handler in the adapter getView() function seems to prevent the ListView onItemClick and onItemLongClick from ever happening.
Below is more detailed context on my application
My goal is to have my ListActivity display a ListView, which functions as follows:
Clicking an item performs a non-selecting action (expands the row to show more info)
Long clicking an item selects it. Selecting an item is indicated by highlighting the background of the row (as in the Gmail app)
You can select multiple items
My application structure is:
Activity is an extension of ListActivity
Adapter is an extension of ArrayAdapter<>
ListView row layouts are completely custom layouts (not any sort of built-in ListView row layout)
My understanding of the built-in functionality for ListView has me to the point where I am
setting choiceMode to multipleChoice
using the ListView 'checked' functionality for making and tracking the selections
using a custom selector as an 'activatedBackgroundIndicator' to show the highlighting (example here)
Keep an ArrayList to maintain ListView items selected position. When a ListView item is selected check in that ArrayList whether item position is in ArrayList or not. If item is not in ArrayList change state of Item to checked else change state to unchecked and remove the position object from ArrayList. This worked for me.

Categories

Resources