It is my third day now dealing with the handling of my view clicks. I originally was using ListView
, then I switched to RecyclerView
. I have added android:onclick
elements to every control on my row_layout
and I am handling them in my MainActivity
like this:
public void MyMethod(View view) {}
In my old ListView
implementation, I have done setTag(position)
to be able to get it in MyMethod
by doing this inside it:
Integer.parseInt(view.getTag().toString())
This worked nicely without problems. Though now I am dealing with RecyclerView
and being forced to use the ViewHolder
, which does not offer a setTag
method. After searching for 2 hours, I have found that people use setTag
like this:
holder.itemView.setTag(position)
This was acceptable. Though when I try to get the value from the MyMethod
function using the line:
Integer.parseInt(view.getTag().toString())
The application crashes. I have read several implementation of onclick handling inside the adapter which works but I have to use the MainActivity
because I am using something that is unique to that activity.
TL;DR I want to send the position of the clicked row to my MainActivity
in a simple manner.
Edit: I apologize for the confusion since my topic was not very thorough. I have a RecyclerView
and an adapter. The adapter is linked to my row_layout
. This row_layout
xml has one root LinearLayout
. Inside it there is one TextView
, another LinearLayout
(which has two TextViews
) and one Button
(for simplicity). I do not want to suffer for dealing with the clicks on RecylerView
like I did with the ListView
. So, I have decided to add an android:onclick
for every control, then link TextView
and LinearLayout
to a single method and link the Button
(and future Button
s) to their unique methods. What I am missing is that I want to be able to tell the position on each of the receiving methods on my MainActivity
. If I must link everything that comes from the adapter and goes into the MainActivity
to a single onclick handler, so be it. Although, how would I tell which control fired the click?
Edit 2: The requested layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:onClick="MyMethod"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/letter"
android:onClick="MyMethod"
android:layout_width="60dp"
android:layout_height="fill_parent"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:onClick="MyMethod"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/firstname"
android:onClick="MyMethod"
android:layout_width="fill_parent"
android:layout_height="17dp" />
<TextView
android:id="@+id/longname"
android:onClick="MyMethod"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:text="Test"
android:onClick="OtherMethod"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/process"/>
</LinearLayout>