我整天都在努力解决这个问题。我有一个简单的AutoCompleteTextview,其中一个建议是洛杉矶。如果用户键入洛杉矶,其中2个单词之间有1个空格,但如果他们输入2个空格或更多,则建议停止。这是我的代码
AutoCompleteTextView location= (AutoCompleteTextView)findViewById(R.id.location);
String[] arrays= {"Los Angeles"};
// Los Angeles: this works
// Los Angeles: this does not work notice the extra space in between
// I want to treat extra spaces as just 1 space
location.setThreshold(2);
ArrayAdapter<String> adapter= new ArrayAdapter<>(Purpose.this, android.R.layout.simple_selectable_list_item,arrays);
location.setAdapter(adapter);
我也试过这个
String[] arrays= {"Los Angeles".replaceAll(" "," ")};
// I can not obviously use trim because it merges everything
我有很多数据组,但这是根本问题,如果我可以修复这部分,那么它就解决了另一组数据的问题。
答案 0 :(得分:1)
我不知道有什么可以做到这一点,我个人只会写一个单独的方法来清理你的String
。涉及 StringBuilder
for(int x = 0; x < YourString.length(); x++) {
if(YourString[x].equals(" ") && YourString[x-1].equals(" ") {
(StringBuilder.removeCharAt(x-1));
}
}
我输入的原始代码,但只是为了给你一些启发。