How can I get access to the, for example, TextView, from the onClick method assigned to the, for example, ImageView? The TextView and ImageView make up the ListView item.
In my item.xml I have:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:onClick="onImgClick"
/>
<TextView android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
And in my ListActivity I have onImgClick method:
public void onImgClick(View v) {
TextView tv = (TextView) v.findViewById(R.id.text);
tv.setText("Hello world!");
}
But the View v – this is ImageView, so in this method I have Fatal Exception:
java.lang.NullPointerException
on the line:
tv.setText("Hello world!");
I know this isn’t correct way to get access to the TextView. And I know that I can use onListItemClick(ListView l, View v, int position, long id) according to the whole ListView. but I want to achieve this from the ImageView onClick method.
I have resolved my problem. I need only to set id on my LinearLayout:
And next do something like that: