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.
Related
I want to pre checked/ pre selected a specific item when a list being created. I use CheckedTextView for the text and checkbox.
For example, I have 3 items, after the list create, I will get a listview. and the second item is already check/tick.
I know how to perform it by using onclick, but what I need is before I click it. some item is already checked.
Thank you
Add a boolean isChecked variable in your ItemModel, set true/false as you wish. Check/uncheck item in your CustomAdapter.
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 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.
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.
I have a spinner populated with custom view by extending BaseAdapter. Items are populated by overriding the getView() method in BaseAdapter
I need to find a way to distinguish the selected item in the drop down list only. I mean when the user clicks on the spinner the item previously selected will show in different color/ background etc.
I don't think there is a need to see the code because it's similar to many on the site.
Couldn't you just use setOnItemSelectedListener? It would record the position of the spinner, and you could find if the position change.
http://developer.android.com/reference/android/widget/AdapterView.html