How to access views within ListView rows? - android

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.

Related

Hide checkboxes when button is clicked

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.

set text color for each custom adapter from a listView

I want to make a diable switch that can instantly gray out and disable a list of options from a listView, but I am not sure how to acchieve this with a CustomAdapter for each single row. With only textview it should be easy to set it with a onclick method, but can I also do that with a CustomAdapter?
Your question is kind of abstract because it doesn't have any code samples and no specific problem as well. So my answer will be abstract as well.
To achieve this set a listener on your switch. Once it's clicked, update the model that is used for displaying your adapter items.
If you need to update all items in the list, then you can add a boolean variable to the adapter class. Use this variable in your getView() method to decide whether the item should be grayed out or not. When the value is changed, call notifyDataSetChanged() on the adapter object. It will trigger redrawing all items in the list.
If you need to update only specific items, then add this boolean variable to the item model itself. Iterate over collection and set this flag where needed. All other logic is the same - use this variable in getView() and call notifyDataSetChanged().

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)

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

Android ListView CHOICE_MODE_MULTIPLE, how to set checked index?

I'm using the cool feature of the ListView to show a checkbox next to the item in the ListView.
I bind my list to an array of strings.
The onClick and onSelectedItem listeners get called fine, in this way I know the index of the "string" checked (or unchecked).
I'm storing all the checked strings into preferences (as a comma-concatenated-string), and everytime the activity becomes visible I would like to set the checked items back in the listview.
Is there a way of doing it? or the CHOICE_MODE_MULTIPLE doesn't allow to set the checked items?
note: I'm not using a custom view, since what I want to display is just a string and a checkbox. I've tried setSelection(index) but it should set the onlyone selected (highlighted) row.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,names);
m_playlists_list.setAdapter(adapter);
m_playlists_list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Use the setItemChecked method of ListView
Sets the checked state of the specified position. The result is only valid if the choice mode has been set to CHOICE_MODE_SINGLE or CHOICE_MODE_MULTIPLE.

Categories

Resources