I'm using this project https://github.com/googlesamples/android-RecyclerView as a starting point to learn about RecyclerView, and implementing the answer by MLProgrammer-Cim here
I'm trying to set the value of 2 strings when a button is pressed. I'm also trying to change the value of the strings depending on the position of the button clicked.
On first click the position is read, but the strings values are not set. On second click, the position is read and strings values are set, according to the position of the button clicked as intended.
What am I doing wrong which is not allowing the values of the string to be set on the first click? Thanks
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private static final String TAG = "CustomAdapter";
...
// String textBoton1;
// String textBoton2;
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private final TextView textView;
private final TextView textView1;
private final ImageView imageOne;
private final Button imageIcon;
public MyViewHolderClicks mListener;
private Context context;
String textBoton1;
String textBoton2;
public ViewHolder(View myv, MyViewHolderClicks listener) {
super(myv);
mListener = listener;
textView = (TextView) myv.findViewById(R.id.textView);
textView1 = (TextView) myv.findViewById(R.id.textView1);
imageOne = (ImageView) myv.findViewById(R.id.image1);
imageIcon = (Button) myv.findViewById(R.id.button_for_cardview);
imageIcon.setOnClickListener(this);
myv.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int position = getLayoutPosition();
if (v instanceof Button){
mListener.onTomato((Button) v, getLayoutPosition());
Log.d(TAG, "Tomato Button onClick position Clicked" + getLayoutPosition()+ " "+ textBoton1+ textBoton2);
switch (v.getId()) {
case R.id.button_for_cardview:
if (position == 0) {
GlobalVariables.getInstance().setRightAnswer1("this is right ");///outanother global in here
textBoton1 = "String text boton one position " + getLayoutPosition();
textBoton2 = " String text boton two position " + getLayoutPosition();
}if (position == 1) {
GlobalVariables.getInstance().setRightAnswer2("this is wrong ");
textBoton1 = "String text boton one position "+ getLayoutPosition();
textBoton2 = "String text boton two position "+ getLayoutPosition();
}
}
} else {
mListener.onPotato(v, getLayoutPosition());
Log.d(TAG, "Potato View onClick position Clicked" + getLayoutPosition() + " " + positionClicked + textBoton1 + textBoton2);
}
}
public interface MyViewHolderClicks {
public void onPotato(View v, int position);
public void onTomato(Button v, int position);
public void onTomato(Button v);
}
public TextView getTextView() {
return textView;
}
public TextView getTextView1() {
return textView1;
}
public ImageView getImageView() {
return imageOne;
}
public Button getImageView1() {
return imageIcon;
}
}
List<Question> questions;
CustomAdapter(List<Question> questions) {
this.questions = questions;
}
public CustomAdapter(String[] dataSet) {
mDataSet = dataSet;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
Log.d(TAG, "Element " + position + " set.");
viewHolder.getTextView().setText(questions.get(position).english);
viewHolder.getTextView1().setText(questions.get(position).spanish);
viewHolder.getImageView().setImageResource(questions.get(position).drawableId);
viewHolder.getImageView1().setBackgroundResource(questions.get(position).iconId);
}
@Override
public int getItemCount() {
return GlobalVariables.getInstance().getPreguntasCorrectas();
}
@Override
public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
//int positionClicked =0;//set this to something like position = getLayoutPosition();
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.text_row_item, viewGroup, false);
CustomAdapter.ViewHolder vh = new ViewHolder(v, new CustomAdapter.ViewHolder.MyViewHolderClicks(){
String textBoton1;
String textBoton2;
public void onPotato(View myv, int position) {
Log.d(TAG,"Potato View"+ " position" + position + textBoton1 +textBoton2);
// switch (position) {
// case 0:
if (position == 0) {
GlobalVariables.getInstance().setRightAnswer1("this is right ONCREATE VIEWHOLDER");///outanother global in here
textBoton1 = "String text boton one from OnCreateViewHolder position "+ position;
textBoton2 = "String text boton two from OnCreateViewHolder position "+ position;
}
// case 1:
if (position == 1) {
GlobalVariables.getInstance().setRightAnswer2("this is wrong ONCREATE VIEWHOLDER");
textBoton1 = "String text boton one from OnCreateViewHolder position "+ position;
textBoton2 = "String text boton two from OnCreateViewHolder position "+ position;
}
}
// }
public void onTomato(Button imageIcon,int position) {
Log.d(TAG, "Tomato Button OnCreateViewHolder" + " position " + position + textBoton1 + textBoton2);
switch (imageIcon.getId()) {
case R.id.button_for_cardview:
if (position == 0) {
GlobalVariables.getInstance().setRightAnswer1("this is right"); textBoton1 = "String text boton one from OnCreateViewHolder position " + position;
textBoton2 = "String text boton one from OnCreateViewHolder position " + position;
}
if (position == 1) {
GlobalVariables.getInstance().setRightAnswer2("this is wrong");
textBoton1 = "String text boton two from OnCreateViewHolder position " + position;
textBoton2 = "String text boton two from OnCreateViewHolder position " + position;
}
}
}
@Override
public void onTomato(Button v) {
}
});
return vh;
}
}
text_row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_medium"
android:layout_marginRight="@dimen/margin_medium"
android:gravity="center_vertical"
>
<!--android:clickable="true"-->
<!--android:focusable="true"-->
<!--android:focusableInTouchMode="true"-->
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cv"
card_view:cardCornerRadius="8dp"
card_view:cardElevation="3dp">
<!--android:focusableInTouchMode="false"-->
<!--android:focusable="false"-->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="16dp"
android:focusable="false"
android:focusableInTouchMode="false" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image1"
android:layout_alignParentTop="true"
android:background="@drawable/as18_boy_point_that_chair"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:text="Sample text for align"
android:layout_toRightOf="@+id/image1"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:text="Sample text for align"
android:layout_below="@+id/textView"
/>
<com.MyApp.app.ButtonPlus
android:id="@+id/button_for_cardview"
style="@style/fafont"
android:layout_width="60dp"
android:layout_height="60dp"
android:textColor="#ffffff"
android:text="@string/icon_mic"
android:textSize="25dp"
android:layout_toRightOf="@+id/textView"
android:background="@drawable/speaker_circle"
android:visibility="visible"
/>
</RelativeLayout>
<!--android:onClick="speechClicked"-->
</android.support.v7.widget.CardView>
<ListView
android:id="@+id/lvVoiceReturn"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="80dp"
android:textColor="#ff33b5e5"
android:layout_marginTop="20dp"
android:visibility="invisible"
android:ems="10" >
</ListView>
logcat output 1st click
07-10 00:26:06.803 29652-29652/? D/CustomAdapter﹕ Tomato Button OnCreateViewHolder position 0nullnull
07-10 00:26:06.803 29652-29652/? D/CustomAdapter﹕ Tomato Button onClick position Clicked0 nullnull
logcat output 2nd click
07-10 00:26:24.353 29652-29652/? D/CustomAdapter﹕ Tomato Button OnCreateViewHolder position 0String text boton one from OnCreateViewHolder position 0String text boton one from OnCreateViewHolder position 0
07-10 00:26:24.353 29652-29652/? D/CustomAdapter﹕ Tomato Button onClick position Clicked0 String text boton one position 0 String text boton two position 0