Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android RecyclerView需要点击2次来设置字符串_Android_Android Recyclerview - Fatal编程技术网

Android RecyclerView需要点击2次来设置字符串

Android RecyclerView需要点击2次来设置字符串,android,android-recyclerview,Android,Android Recyclerview,我将这个项目作为学习RecyclerView的起点,并通过MLCIM程序员实现答案 我试图在按下按钮时设置两个字符串的值。 我还试图根据单击的按钮的位置更改字符串的值 第一次单击时,读取位置,但未设置字符串值。第二次单击时,将根据所单击按钮的位置读取位置并设置字符串值 我做错了什么,不允许在第一次单击时设置字符串的值?谢谢 public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder

我将这个项目作为学习RecyclerView的起点,并通过MLCIM程序员实现答案

我试图在按下按钮时设置两个字符串的值。 我还试图根据单击的按钮的位置更改字符串的值

第一次单击时,读取位置,但未设置字符串值。第二次单击时,将根据所单击按钮的位置读取位置并设置字符串值

我做错了什么,不允许在第一次单击时设置字符串的值?谢谢

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;
}

}
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

您的
Log.d
在第一次单击设置
textBoton1
textBoton2
变量之前发生。。。在第一次单击设置
textBoton1
textBoton2
变量之前,请尝试将其移动到switch语句后的
Log.d
。。。试着在switch语句之后将其移动到

facepalm,是的,就是这样..我已经看了好几个小时了..哈哈,谢谢..继续回答吧,我会把它标记为正确答案facepalm,是的,就是这样..我已经看了好几个小时了..哈哈,谢谢..继续回答吧,我会把它标记为正确答案
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
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