![](/img/trans.png)
[英]android listview gettag nullpointexception in imageview onclicklistener
[英]Android OnClickListener in an ImageView of ListView
我的布局中有一个ListView
。 为了显示其中的清单,我使用了以下代码。
ListView shopActivityListView = (ListView) findViewById(R.id.shopActivityListView);
shopActivityListView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.shopitem, R.id.text1,trackArr[1]));
这是shopitem.xml
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>
现在,我想在每个项目的ImageView
上添加一个onClickListener
。 如何添加?
您必须实现ArrayAdapter并将onclicklistners放入GetView。 这是自定义列表视图的链接: http : //www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
GetView函数在已实现的ArrayAdapter类下。
实现OnItemClickListener
并将其设置为listView。 使ImageView在android xml中可点击。
在onItemClick
方法中,检查ImageView ID,您应该一切顺利。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.