![](/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.