Android OnClickListener in an ImageView of ListView
I have a ListView
in my layout. To display the listing in it, I have used the following code.
ListView shopActivityListView = (ListView) findViewById(R.id.shopActivityListView);
shopActivityListView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.shopitem, R.id.text1,trackArr[1]));
开发者_如何学编程Here is xml for shopitem.xml
<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="2dip"
android:paddingBottom="2dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:mode="twoLine"> <!-- android:background="@drawable/homebg" -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="44dip"
android:textStyle="bold"
android:lines="1"
android:textColor="#FFFFFF"/>
<ImageView
android:id="@+id/buyBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="3dip"
android:layout_marginLeft="252dip"
android:layout_alignRight="@android:id/text1"
android:src="@drawable/btnbuy"/>
</TwoLineListItem>
Now I want to add a onClickListener
on the ImageView
of each item. How can I add it?
You have to implement ArrayAdapter and put onclicklistners in GetView. Here is a link to custom listview : http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
GetView function is under implemented ArrayAdapter class.
Implement the OnItemClickListener
and set it up for the listView. Make the ImageView clickable in the android xml.
In the method onItemClick
check for the ImageView id and you should be good to go..
精彩评论