How to higlight multiple items in a list in Android? - android

I have a ListActivity that presents a list of names from a database using a SimpleCursorAdapter. I want the user to be able to select 1 or more names by clicking them and then proceed to the next Activity. This should be a toggle, so that if the user clicks a selected name it will become de-selected. The underlying code is working fine, the problem is how to show the user which items are currently selected.
I looked at this solution: Android how to highlight a selection in a list and tried toggling .setSelected() on the TextView for the name. The problem is that the "selected" state apparently can only be true for one item in a list at a time. So if the user touches "Alice" then "Bob," only "Bob" will show as selected. Any thoughts on the best way to have a toggle-able highlight for multiple list items?

Have a look at the setChoiceMode method of the AbsListView class and its possible CHOICE_MODE_MULTIPLE parameter value.

Related

Android open ListView for multiple selection programmatically

I got a fragment with a ListView. The purpose of the list is simply checking items in that list, so I want to add something to the onCreateView method which will make the list enter its multiple selection mode automatically as the fragment shows, without the need for the user to long press an item. How can I do that?
to use longpress , you can do this way,
android:choiceMode="multipleChoiceModal"
or
setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL)
go through this for more information

Highlighting selected view in Listview Android

I have a listview and a corresponding listview adapter.
The views displayed in the Listview have I written myself and it's a framelayout with one button and one imagebutton. The imagebutton is a red cross that deletes the entry and the regular button selects the entry.
When the user presses the regular button, that item is selected and I want to show this to the user by setting the background of that item to green. The application stores which item the user selected and the next time the listview is rendered, THAT item should be selected and green.
Notice that there should be one and EXACTLY one selected item in my listview at all times.
In the getView(.. method in my adaper, it's very easy to change color of the button when the user clicks it. But the button that was green before the user clicked is impossible for me to reference.
I tried storing a reference the previously selected button, but it never repainted
I tried removing and adding the data item from the list to trigger notifyDataSetChanged but it never repainted
I tried setChoiceMode(ListView.CHOICE_MODE_SINGLE), but that led nowhere because I don't know how to catch the choice in my getView method and paint differently depending on wheter it's selected or not.
All guides I see suggest using notifyDataSetChanged. However, the underlying data is NOT changed in this case and it is not correct (or possible) solution.
So I think that my problem boils down to: HOW can I reference another view in my listview??
(And Yes, I have seen this post: Highlight selected item in ListView on Android . It describes my problem and is answered with "and then change the color of previous selected item's background back to normal" but I still can't refer to the PREVIOUS selected item.
Thanks!
Why don't you use Radio Buttons ? This is exactly the design you need. You can add a "red cross" on each line to delete the line. The selected line is unique and you can iterate on RadioButton.isChecked(); of your RadioGroup to set the background color accordingly.

spinner selected item is highlighted while in drop down mode android

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

how to keep check box selection in ListView

I have created a custom ListView.. every element of my ListView constructing from a layout.
My layout will have some TextViews and a ToggleButton. My ListView will have some 50 elemnts and my device screen can show 2 elements at a time.
Now if we select ToggleButton on a Item and scroll down my ListView , the selected item will be out of screen and when I come back to the previous selected item I am observinf that selection for that toggle button is gone.
I know that this is a normal behavior in android that It will construct the elements which is currently displaying.
But I want to keep that value.. Is there any way that we preserve selection. or can I tell ListView to donot construct every time.. memory is a not a issue for my application.
Please let me know if there's any good solution for this.
You could store the states of the checkbox in a map with the position as the key. So whenever the list reloads after scroll, it loads based on the hashmap state. You should write this logic in the getView()

List view row Selection

i have a simple list view control
my queries are:
1) how to set first row selected on start (after fill data in list view)
2) when i navigate by hardware button i got AdapterView.OnItemSelectedListener and color of row background change, but when i click i not get any OnItemSelectedListener and no row selected. How to select row on click.
3) when i change focus list row selection removed,
please any one with any solution, code example or articles
i search but no susses
with regards
Chandra Kant Singh
how to set first row selected on start
(after fill data in list view)
Call setSelection() on the ListView. However, this only will have a visual effect if the user entered your activity via the pointing device (trackball, D-pad, etc.).
How to select row on click.
You don't.
when i change focus list row selection
removed,
If I understand what you mean by "change focus", this is working as designed.
Please review this article on touch mode and the use of the "selected" state, then adjust your UI design to follow Android conventions. Selection is only used when the user navigates with the pointing device, not when the user uses the touch screen.

Categories

Resources