Reputation: 868
So I have the following fuction inside an adapter class. I want my Toast to print the text of a TextView when a certain icon is clicked but turns out I'm unable to get that TextView inside of my onClick function.
public TitleParentViewHolder onCreateParentViewHolder(ViewGroup viewGroup) {
View view = inflater.inflate(R.layout.list_routines, viewGroup, false);
ImageView playIcon = (ImageView) view.findViewById(R.id.playRoutine);
playIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView rutNameView = (TextView) v.findViewById(R.id.parentTitle);
String rutName = rutNameView.getText().toString(); //THIS crashes because rutNameView is null
Toast.makeText(v.getContext(), rutName + " was played",
Toast.LENGTH_LONG).show();
}
});
return new TitleParentViewHolder(view);
}
I've tried defining rutName outside of teh onClick but its value changes so that doesn't make sense (it's first initalized as "" and then assigned a value that I get from an API).
The text I'm trying to get is the one inside of the TextView with id parentTitle in a CardView. I have several of these cardViews. I don't know if it's relevant but here's the code.
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginTop="5dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/playRoutine"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_margin="8dp"
android:src="@drawable/play_gris"
android:visibility="visible" />
<TextView
android:id="@+id/parentTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="16dp"
android:textSize="20dp"
android:textStyle="bold" />
<ImageButton
android:id="@+id/expandArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="8dp"
android:src="@drawable/expande_de_lista"
android:visibility="visible" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Upvotes: 0
Views: 59
Reputation: 3001
Do this -
public TitleParentViewHolder onCreateParentViewHolder(ViewGroup viewGroup) {
View view = inflater.inflate(R.layout.list_routines, viewGroup, false);
ImageView playIcon = (ImageView) view.findViewById(R.id.playRoutine);
TextView rutNameView = (TextView) view.findViewById(R.id.parentTitle);
playIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String rutName = rutNameView.getText().toString();
Toast.makeText(v.getContext(), rutName + " was played",
Toast.LENGTH_LONG).show();
}
});
return new TitleParentViewHolder(view);
}
Upvotes: 2
Reputation: 7493
Write below declaration code before playIcon.setOnClickListener
TextView rutNameView = (TextView) v.findViewById(R.id.parentTitle);
Upvotes: 0