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
Related
I am using a ListView in combination with a HashList to show data from an SQL Database through JSON. Everything works fine. But now I want to have a special feature.
In the ListView are round about 400 Items. So if i click one of the item to view its content and switch via Backbutton back to the Listview, i want to have the viewed Item from the Listview being marked in another color so that the user sees directly which oh the items he already has clicked.
I painted an little grafik for you, for better understanding
On the left side, the original View. After i View the 1st,5th and 8th Item of the list, the list should look like at the right side.
Is this possible?
You can implement your own adapter, then have an boolean array to store the current status of every row, and in the getView function assign a different color to the background of the selected rows based on your boolean array.
For this you will need also to set an onClick event in your rows to open the previsualization view and there also check the boolean array as "visited".
For more info about how to make custom efficient adapters, please read this
To simply let the user choice more than one row you can use
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
in your list
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 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.
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.
I have a ListView and I want to select multiple items in the ListView without using CheckedTextView.
Please help !!
You don't really need a CheckBox. All you need is a boolean flag on your list objects. When the user clicks an item you change the state of the flag, and probably the background or text color for that list item.
I never tried it, but I would use ListView with checkbox but non visible.
Perform the setChecked(True) when the user press on an item.
I would also try changing the item background when the checkbox is checked.