Android 比较editText的文本时出错
此代码尝试捕获EditText的字符串,并将其与数组的字符串进行比较。 代码没有错误,但是当我点击按钮时什么都没有发生(甚至没有显示祝酒词),我做错了什么? 谢谢 这是数组的te数组。 list.xmlAndroid 比较editText的文本时出错,android,arraylist,compare,android-edittext,Android,Arraylist,Compare,Android Edittext,此代码尝试捕获EditText的字符串,并将其与数组的字符串进行比较。 代码没有错误,但是当我点击按钮时什么都没有发生(甚至没有显示祝酒词),我做错了什么? 谢谢 这是数组的te数组。 list.xml 1. 第一 2. 第二 @阵列/阵列1 @阵列/阵列2 这是主xml文件的代码 main.xml 搜索数组 public void searchArray(View view){ Resources res = getResources(); TypedA
1.
第一
2.
第二
@阵列/阵列1
@阵列/阵列2
这是主xml文件的代码
main.xml
搜索数组
public void searchArray(View view){
Resources res = getResources();
TypedArray ta = res.obtainTypedArray(R.array.list);
int n = ta.length();
String[][] array = new String[n][];
for (int i = 0; i < n; ++i) {
int id = ta.getResourceId(i, 0);
if (id > 0) {
array[i] = res.getStringArray(id);
} else {
// something wrong with the XML
}
}
ta.recycle();
TextView name = (TextView) findViewById(R.id.etname);
String stringname = name.getText().toString();
if (stringname != null){
for (int cont = 0; cont < n; ++cont) {
if (!(array[cont][1].equals(stringname))){
Toast toast2 = Toast.makeText(getApplicationContext(), array[cont][1], Toast.LENGTH_SHORT);
toast2.show();
} else {
Toast toast = Toast.makeText(getApplicationContext(), "WRONG", Toast.LENGTH_SHORT);
toast.show();
}
}
}
}
publicsvoidsearcharray(视图){
Resources res=getResources();
TypedArray ta=res.obtainTypedArray(R.array.list);
int n=ta.length();
字符串[][]数组=新字符串[n][];
对于(int i=0;i0){
array[i]=res.getStringArray(id);
}否则{
//XML有问题吗
}
}
ta.recycle();
TextView名称=(TextView)findViewById(R.id.etname);
String stringname=name.getText().toString();
if(stringname!=null){
对于(int cont=0;cont
看起来您是从索引1进行比较,而不是从0进行比较。更改代码
for (int cont = 0; cont < n; ++cont)
for(int cont=0;cont
到
for(int cont=0;cont
像这样试试
public void searchArray(View view){
Resources res = getResources();
TypedArray ta = res.obtainTypedArray(R.array.list);
int n = ta.length();
String[][] array = new String[n][];
for (int i = 0; i < n; i++) {
int id = ta.getResourceId(i, 0);
if (id > 0) {
array[i] = res.getStringArray(id);
System.out.println(res.getStringArray(id));
} else {
// something wrong with the XML
}
}
ta.recycle();
EditText name = (EditText) findViewById(R.id.etname);// I changed TextView to EditText
String stringname = name.getText().toString();
if (stringname != null){
for (int cont = 0; cont < n; ++cont) {
System.out.println(array[cont][1]+" "+stringname);
if ((array[cont][1].equals(stringname))){//Edit Here, If it matches, Show Toast
Toast toast2 = Toast.makeText(getApplicationContext(), array[cont][1], Toast.LENGTH_SHORT);
toast2.show();
} else {
Toast toast = Toast.makeText(getApplicationContext(), "WRONG", Toast.LENGTH_SHORT);
toast.show();
}
}
}
}
publicsvoidsearcharray(视图){
Resources res=getResources();
TypedArray ta=res.obtainTypedArray(R.array.list);
int n=ta.length();
字符串[][]数组=新字符串[n][];
对于(int i=0;i0){
array[i]=res.getStringArray(id);
System.out.println(res.getStringArray(id));
}否则{
//XML有问题吗
}
}
ta.recycle();
EditText name=(EditText)findViewById(R.id.etname);//我将TextView更改为EditText
String stringname=name.getText().toString();
if(stringname!=null){
对于(int cont=0;cont
希望这对您有所帮助。下面是Szymon的精彩观察。我还注意到,不管你在问题中说了什么,你都在与TextView而不是EditText进行比较。这不是错误,我更改了它,它不起作用。我认为错误在for中,但不在for中,因为它应该显示toast“错了”,但什么都没有发生
n
的值是什么?是的,我知道,但调试时的值是什么。
for (int cont = 0; cont < n; ++cont)
for (int cont = 0; cont < n; cont++)
public void searchArray(View view){
Resources res = getResources();
TypedArray ta = res.obtainTypedArray(R.array.list);
int n = ta.length();
String[][] array = new String[n][];
for (int i = 0; i < n; i++) {
int id = ta.getResourceId(i, 0);
if (id > 0) {
array[i] = res.getStringArray(id);
System.out.println(res.getStringArray(id));
} else {
// something wrong with the XML
}
}
ta.recycle();
EditText name = (EditText) findViewById(R.id.etname);// I changed TextView to EditText
String stringname = name.getText().toString();
if (stringname != null){
for (int cont = 0; cont < n; ++cont) {
System.out.println(array[cont][1]+" "+stringname);
if ((array[cont][1].equals(stringname))){//Edit Here, If it matches, Show Toast
Toast toast2 = Toast.makeText(getApplicationContext(), array[cont][1], Toast.LENGTH_SHORT);
toast2.show();
} else {
Toast toast = Toast.makeText(getApplicationContext(), "WRONG", Toast.LENGTH_SHORT);
toast.show();
}
}
}
}