OnClick脚本出错

时间:2015-01-31 12:33:48

标签: java android onclick android-recyclerview

    myOnClickListener = new MyOnClickListener(this);
    nRecyclerView = (RecyclerView) findViewById(R.id.my_recycler2_view);
    nRecyclerView.setHasFixedSize(true);

static class MyOnClickListener implements View.OnClickListener{

    private final Context context;
    private MyOnClickListener(Context context) {
        this.context = context;
    }

    public void onClick(View v) {
        checkWhich(v);
    }

    private static void checkWhich (View v){
        int selectedItemPosition = nRecyclerView.getChildPosition(v);
        RecyclerView.ViewHolder nViewHolder = nRecyclerView.findViewHolderForPosition (selectedItemPosition);

        TextView textViewName = (TextView) nViewHolder.itemView.findViewById(R.id.textViewCountry);
        String selectedName = (String) textViewName.getText();
        for (int i = 0; i  < MyData.countryArray.length; i++){
            if (selectedName==MyData.countryArray[i]) {
                System.out.println(selectedName);
            }
        }
    }
}

这之前已经奏效,但现在还没有奏效。我做错了什么吗?我有2个RecyclerViews但它们在一起不可见。此单击侦听器仅适用于一个RecyclerView

logcat的

01-31 18:41:17.514    3441-3441/com.wc.gap.worldcupfixture I/art﹕ Late-enabling -Xcheck:jni
01-31 18:41:17.761    3441-3471/com.wc.gap.worldcupfixture D/OpenGLRenderer﹕ Render dirty regions requested: true
01-31 18:41:17.767    3441-3441/com.wc.gap.worldcupfixture D/Atlas﹕ Validating map...
01-31 18:41:17.792    3441-3471/com.wc.gap.worldcupfixture I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/24/14, 167c270, I68fa98814b
01-31 18:41:17.793    3441-3471/com.wc.gap.worldcupfixture I/OpenGLRenderer﹕ Initialized EGL, version 1.4
01-31 18:41:17.805    3441-3471/com.wc.gap.worldcupfixture D/OpenGLRenderer﹕ Enabling debug mode 0
01-31 18:41:57.529    3441-3470/com.wc.gap.worldcupfixture I/System.out﹕ 544, 543, 646, 797, 64, 66, 987
01-31 18:43:25.218    4242-4271/com.wc.gap.worldcupfixture D/OpenGLRenderer﹕ Render dirty regions requested: true
01-31 18:43:25.224    4242-4242/com.wc.gap.worldcupfixture D/Atlas﹕ Validating map...
01-31 18:43:25.257    4242-4271/com.wc.gap.worldcupfixture I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/24/14, 167c270, I68fa98814b
01-31 18:43:25.258    4242-4271/com.wc.gap.worldcupfixture I/OpenGLRenderer﹕ Initialized EGL, version 1.4
01-31 18:43:25.270    4242-4271/com.wc.gap.worldcupfixture D/OpenGLRenderer﹕ Enabling debug mode 0

1 个答案:

答案 0 :(得分:0)

实施View.OnClickListener时,请执行以下操作。

public class YourClass extends Activity implements View.OnClickListener

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //Component setup here. 
        nRecyclerView = (RecyclerView) findViewById(R.id.my_recycler2_view);
        nRecyclerView.setHasFixedSize(true);
        nRecyclerView.setOnClickListener(this);
    }    

    @Override
    public void onClick(View v) {
        checkWhich(v);
    }

    private static void checkWhich (View v){
        int selectedItemPosition = nRecyclerView.getChildPosition(v);
        RecyclerView.ViewHolder nViewHolder = nRecyclerView.findViewHolderForPosition (selectedItemPosition);

        TextView textViewName = (TextView) nViewHolder.itemView.findViewById(R.id.textViewCountry);
        String selectedName = textViewName.getText().toString();
        for (int i = 0; i  < MyData.countryArray.length; i++){
            if (selectedName==MyData.countryArray[i]) {
                System.out.println(selectedName);
            }
        }
    }
}