我正在使用这个项目https://github.com/googlesamples/android-RecyclerView作为了解RecyclerView的起点,并通过MLProgrammer-Cim实现答案here
按下按钮时,我正在尝试设置2个字符串的值。 我也试图根据点击按钮的位置来改变字符串的值。
首次单击时,将读取位置,但未设置字符串值。在第二次单击时,将根据按预期单击按钮的位置读取位置并设置字符串值。
我做错了什么,不允许在第一次点击时设置字符串的值?感谢
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输出第一次单击
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输出第二次点击
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
答案 0 :(得分:0)
在您第一次点击设置Log.d
和textBoton1
变量之前,您的textBoton2
正在发生...尝试将其移至切换语句后