我显示来自以下字符串的 ListView :
String[] values = new String[] { test1, test2, test3 };
变量:
private String test1 = "test";
private String test2 = "test";
private String test3 = "test3";
现在我不想在 ListView 中显示包含“test”的字符串。 像这样:
if (String == "test") {
*don't show in ListView*;}
我希望它一次测试所有包含“test”的字符串
这怎么可能?
编辑:这里是适配器代码:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
最佳答案
是的...
if (values[i].equals("test")) // i being the iterator of your for-loop
或
if (values[i].contains("test"))
contains 是一个较慢的(呃)字符串函数。
虽然你可能想要使用 ArrayList...这样你就可以将所有这些对象添加到数组列表中,然后遍历它...并在进行时删除它们...
// Do something to add all items to your array list
...
// Iterate through the list, removing what doesn't need to be there
for (int i = 0; i < arrayList.size(); i++){
if (arrayList.get(i).contains("test"))
arrayList.remove(i);
}
...然后将“arrayList”(或任何您所称的名称)设置为您的适配器的字符串列表。如果您使用相同的构造函数,它将看起来像这样......
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, arrayList.toArray());
关于安卓 ListView : don't show all strings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11849186/