I have a ListView
, and for each row of the ListView
I have an ImageView
, 2 TextView
views, and 1 ToggleButton
.
How can I access the views within a row (e.g. the ToggleButton
)?
I've tried using an OnClickListener
, but it didn't work and I don't know why. Below is what I've tried:
@Override
public void onListItemClick(ListView parent, View view, int position, long id){
ToggleButton tb2 = (ToggleButton)parent.findViewById(R.id.star);
if(tb2.isChecked()){
Toast.makeText(MainActivity.this, "TOOGLE BUTTON TEST", Toast.LENGTH_LONG).show();
}
}
My row layout:
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/list_selector_background"
android:clickable="true"
android:gravity="fill_horizontal"
android:longClickable="true"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingBottom="8dip"
android:paddingTop="5dip" >
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:scaleType="fitXY"
android:src="@drawable/contact_user" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="fill"
android:orientation="vertical" >
<TextView
android:id="@+id/nameClient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff" />
<TextView
android:id="@+id/accountClient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#483ca0" />
</LinearLayout>
<ToggleButton
android:id="@+id/star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:background="@drawable/list_star_selector"
android:clickable="true"
android:enabled="true"
android:inputType="none"
android:textOff=""
android:textOn="" />
</TableRow>
try
ToggleButton tb2 = (ToggleButton)view.findViewById(R.id.star);
ListView parent
is the parent view, View view
is the view you actually clicked and contains the subviews you need