
Lets say you have a spinner widget, like the one we created in our 'colours' example, and you wanted to put a button on each row of your spinner for your users to click (everyone loves clicking buttons, right?).
You might have a layout a bit like this simple one below:
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:text="this is a row"
android:id="@+id/tvViewRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<Button android:text="Click me!"
android:id="@+id/BtnToClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler">
</Button>
</LinearLayout>
.. Containing just a textView and a Button View.
A listView with textual, non-clickable views in it responds to Click events via the OnItemClickListener event.
But once you put a button in the ListView, this event no longer fires.
So how do you capture the button's click event, and find what row in your ListView a clicked button is located in?
You may have noticed this
android:onClick="myClickHandler"> in our layout above..What this does is tie every click of every instance of that button, in every row of our ListView to one single handler, located in the code of our ListActivity class below:
public void myClickHandler(View v)
{
//reset all the listView items background colours //before we set the clicked one..
ListView lvItems = getListView();
for (int i=0; i < lvItems.getChildCount(); i++)
{
lvItems.getChildAt(i).setBackgroundColor(Color.BLUE);
}
//get the row the clicked button is in
LinearLayout vwParentRow = (LinearLayout)v.getParent();
TextView child = (TextView)vwParentRow.getChildAt(0);
Button btnChild = (Button)vwParentRow.getChildAt(1);
btnChild.setText(child.getText());
btnChild.setText("I've been clicked!");
int c = Color.CYAN;
vwParentRow.setBackgroundColor(c);
vwParentRow.refreshDrawableState();
}
In this case, the
View v being passed in as a parameter is our button.We get the parent of our button, being careful to cast it as a LinearLayout (have another look at the xml if you're not sure why), and then simply set the background colour of our Layout Row.
Don't forget to call
.refreshDrawableState(); on your vwParentRow or it will never redraw and you won't see your nice colour change... Tada!

Update: Hi everyone, thanks for all the interest, here's a link to the full project zipped.
Updated Update: Here's another link, and another, and another.
I've also updated the above myClickHandler to loop through the other items in the ListView and reset them to a default colour, in this case blue, to make it a bit more obvious what's going on. Hope that helps.








