android change view of selected item in adapterview - android

I have a listview with a textview as each item. I want to change the textcolor of the selected item. For this use the onItemSelected method to make the changes. Say first I select the 1st row, the first row's textcolor changes. Now when I select the 2nd row, its text color also changes, but I want the 1st row's color to change back to its default color. How do I do that, since in the onItemSelected I only get the refernce of the currently selected item and not the last selected. Is there any way other than holding a reference to the last selected view.

When first item is selected store its position in instance variable of your activity, lets name it currentlySelected. Then combine with this Android: Access child views from a ListView
in order to get the view at position currentlySelected and change it's textcolor.

Instead of doing it in Java, you can let android handles it in the xml file. See Color State List Resource.

Related

ListView highlight unselected rows

Scenario.
Say list view has 5 items. When I click on a particular item, that particular item gets highlighted. When i click on another item, the previous item is unselected and the new item is highlighted.
I am able to do so by holding 2 views. CurrentSelectedItemView and PreviousSelectedItemView. Using these two views I am changing the text color and backgrounds of these two rows. But how do i change the color and background of other rows. I do not want to reset the adapter every time a row is selected.
Say text color is black and background is white. When a row is selected,the colors reverse. For other rows,I need to change the text color to grey. I able to do fr 2 rows only... current and previous.
Is there any simple way of doing this?
You should keep track of last selected index to change the background of previous selected item.
To make changes in listview you need to alteast called adapter.notifydatachanged()

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.

How to scroll ListView to a particular position and change the text color

There is custom view and a ListView, both are coupled as such if one changes its state other must change. I am able to control custom view if one item of ListView is selected.
If a item is selected in custom view, corresponding item in List view should become visible and text in it must be hilighted.
I tried
listView.smoothScrollToPosition(clueIndex);
no scrolling happens on execution of this line.
Second question -
How can I get access to TextView in a particular adaptor postion and change its text color outside OnItemLongClickListener() ?
Issue resolved, due to error clueIndex was always -1.

Spinner resets its value when scrolling up

I have a ListView, with different custom rows.
The last row contains a TextView and a Spinner. The Spinner works ok (when clicked the dropdown list is shown, when I choose an option it appears in the Spinner's prompt), but when I scroll up the list, the Spinner changes its value to the value in the first position (ie the string at position 0).
Why is this happening? And what can I do to stop this?
it will its the property create custonbaseadapter set getview and newview method
Advance List
Custom Apdapter
See this

List element hightlight?

I have a list and when I do setOnClickListener for an Item I don't have highlight on Items in a List.
That is not expected behavior because I want to give user a way to see which item was selected on touch.
Is there some way to make it highlighted?
tnx.
Probably you can try to set color to your list item xml. Define your list item color depending on the state. Try this out.

Categories

Resources