Reputation: 36653
I'm trying to highlight a selected row in an Android quiz program. However, I'm getting some odd behavior. Basically, the position passed into onListItemClick correctly reflects the position in the backing data array, but the view, apparently looked up using the child index, is wrong. The child index seems to be the reverse of position, like a stack. Using this assumption, I can make the code work, but it's counter-intuitive.
Here's the code:
public class QuestionActivity extends ListActivity {
...
/*
* Create the adapter
*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.simple_list_item_single_choice_custom,
mAppState.mAnswerArray) {
};
/* attach the adapter to the ListView */
setListAdapter(adapter);
/* set ListView's choice mode to single */
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
....
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// always four rows; assume last row is zeroth child. Therefore,
// if the first row is chosen (0 position) you need to get child 3.
// 3 - 0 = 3.
int childCount = l.getChildCount();
int lastViewPos = childCount - 1;
View selView = l.getChildAt(lastViewPos - position);
}
...
<TextView android:id="@+id/display_question"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="40sp" android:typeface="normal" android:padding="10dp"
android:layout_gravity="center_vertical|center_horizontal"
android:textColor="@color/text_color" />
/>
<ListView android:id="@+id/android:list"
android:cacheColorHint="#00000000" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:textColor="@color/text_color"
android:divider="#00000000" android:dividerHeight="0sp"
android:focusable="false">
</ListView>
<TextView android:id="@+id/display_english"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:paddingLeft="20dp" android:layout_gravity="center_vertical|center_horizontal"
android:textSize="30sp" android:textColor="@color/text_color" />
<Button android:id="@+id/next_button1" android:text="@string/next_question"
android:textSize="15dp" android:layout_gravity="center_vertical|center_horizontal"
android:textColor="@color/text_color" android:background="@drawable/custom_button"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
... Here's the simple_list_item_single_choice_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingLeft="20dip"
android:textColor="@color/text_color"
android:background="@drawable/custom_list"
/>
I also tried not calling the super on the onClick event, and setting the stackFromBottom on the list view from the code, both true and false, but none of those seemed to make any difference.
What's a more satisfactory, intuitive way to do this?
Upvotes: 0
Views: 520
Reputation: 5747
Man, I can't believe I missed this. Wasn't paying proper attention to the actual question.
You're trying to set the value of selView to the item that was clicked, right? That's what the "View v" parameter of onListItemClicked is.
View selView = v;
will do what you're looking for.
getChildAt() doesn't behave the way you're expecting because it's inherited from ViewGroup; in a ListView it doesn't necessarily correspond to just the list items because there may be other things in the ViewGroup (for example, headers and footers). In short, it wasn't meant to be used the way you're using it. That it inversely corresponds to the positions in this case is probably just a coincidence.
Upvotes: 1
Reputation: 19189
It should be set to start from the top, but it starts at 1 (unlike array indexes and such).
Upvotes: 0
Reputation: 5747
Any chance you have android:stackFromBottom specified? I don't know if that would change how getChildAt behaves (and I don't have anything to test on at the moment), but it would be my first guess.
Upvotes: 0