OnClick 脚本错误
Error in OnClick Script
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 个 RecyclerView,但它们不在一起可见。此点击侦听器仅适用于一个 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
实施 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);
}
}
}
}
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 个 RecyclerView,但它们不在一起可见。此点击侦听器仅适用于一个 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
实施 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);
}
}
}
}