I spent several days trying to solve the problem that I have with ListViews on Android. I would like to implement a single select list using a ListView. So, I would like to have only one line with a predefined background color, and the rest with another highlighted color. The problem is that when I click on a specific line, this is another line that is highlighted, and not the one I clicked. I added a few messages to the log of what is happening, but everything is working fine. Here is my code:
public class TryListViewActivity extends Activity { protected static final int NO_SELECTED_COLOR = 0xFF191919; protected static final int SELECTED_COLOR = 0xFF3366CC; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ListView listView = new ListView(this); ArrayList<String> list = new ArrayList<String>(); list.add("Option 1"); list.add("Option 2"); list.add("Option 3"); list.add("Option 4"); list.add("Option 5"); list.add("Option 6"); list.add("Option 7"); list.add("Option 8"); list.add("Option 9"); list.add("Option 10"); list.add("Option 11"); list.add("Option 12"); list.add("Option 13"); list.add("Option 14"); list.add("Option 15"); ArrayAdapter<String> listAdapter = new ArrayAdapter<String>( this, R.layout.list_box_entry, list ); listView.setAdapter(listAdapter); listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
Inside the resources (res / layout) I inserted a file called list_text_entry.xml with the following contents
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:textColor="#FFFFFF" android:padding="10dp" android:textSize="16sp"> </TextView>
For example, if I clicked on the โOption 11โ item, when the listView previously scrolled until the first line that I saw was โOption 4โ, the line โOption 7โ would appear as the selected one, which would be the only blue background in background mode. Can someone explain what is happening to me here? I post below the posts that I have selected for the magazine.
[SingleSelectionListBox] Item clicked: position=10;id=10 [SingleSelectionListBox] 15 items found inside the parent [SingleSelectionListBox] 11 views found inside the parent [SingleSelectionListBox] Child 0 has message Option 4 [SingleSelectionListBox] Child 1 has message Option 5 [SingleSelectionListBox] Child 2 has message Option 6 [SingleSelectionListBox] Child 3 has message Option 7 [SingleSelectionListBox] Child 4 has message Option 8 [SingleSelectionListBox] Child 5 has message Option 9 [SingleSelectionListBox] Child 6 has message Option 10 [SingleSelectionListBox] Child 7 has message Option 11 [SingleSelectionListBox] Child 8 has message Option 12 [SingleSelectionListBox] Child 9 has message Option 13 [SingleSelectionListBox] Child 10 has message Option 14 [SingleSelectionListBox] First visible position: 3 [SingleSelectionListBox] Text inside the View changing the color Option 11
How can I guess that all the children inside the ViewGroup are ordered from top to bottom and even when I do this in code:
TextView textView = (TextView)(parent.getChildAt( position-parent.getFirstVisiblePosition() )); textView.setBackgroundColor(SELECTED_COLOR);
The message Option 11 appears, but option 7 actually selected. Is this a bug from Android?