我有一个 TextView 类型的列表“allPay”。我需要在此列表中找到一个字符串“abc”。我已经使用“contains()”从此列表中找到一个字符串,但即使列表包含此字符串,我也总是得到“false”作为答案。
代码如下:
TextView tv_method_name = (TextView) findViewById(R.id.tv_method_name);
tv_method_name.setText("abc");
tv_method_name.setText("abcde");
List<TextView> allPay = new ArrayList<TextView>();
allpay.add(tv_method_name);
String str = "abc";
System.out.println("====allpay contains string?:" +
allPay.contains("abc"));
结果:
====allpay 包含字符串?:false
最佳答案
您必须使用 for 循环或 for each 循环遍历 TextView 列表,以检查文本。只需使用此方法:
boolean listContains(List<TextView> txtViews, String textToCheck) {
for (TextView txtView : txtViews) {
if (txtView.getText().toString.contains(textToCheck)) {
return true;
}
}
return false;
}
然后只需使用:
if (listContains(allPay, "abc")) {
//"abc" is contained within one of the lists, do something
}
关于java - 如何在 List<TextView> android 中查找字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37926914/