我正在尝试过滤网格视图,为此我在编辑框中输入数据。它工作得很好,除了当我在编辑框中插入空格字符时,它以某种方式无法识别它并且结果集变空。请帮忙
这是我的编辑盒监听器:
search.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {}
@Override
public void onTextChanged(CharSequence cs, int start, int before, int count) {
MainActivity.this.adapter.getFilter().filter(cs);
}
});
我无法弄清楚问题,因为它与其他角色一起工作正常。
答案 0 :(得分:1)
尝试使用trim()
。希望它对你有用。我知道你需要修改这段代码。但它可能会给你一些想法。
search.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
MainActivity.this.adapter.getFilter().filter(search.getText().toString().trim());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence cs, int start, int before,
int count) {
// TODO Auto-generated method stub
}
});
如果这不起作用,请尝试第二个选项
search.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
String result = s.toString().replaceAll(" ", "");
if (!s.toString().equals(result)) {
ed.setText(result);
ed.setSelection(result.length());
// alert the user
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence cs, int start, int before,
int count) {
}
});
我更喜欢使用trim()
的第一个选项来标记文字。
答案 1 :(得分:0)
将您的过滤器代码移至afterTextChanged()
方法。
例如:
search.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
MainActivity.this.adapter.getFilter().filter(search.getText().toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence cs, int start, int before,
int count) {
// TODO Auto-generated method stub
}
});
答案 2 :(得分:0)
我能够使用以下代码解决此问题:
@Override
public void afterTextChanged(Editable editable) {
String keywords = editable.toString();
if (keywords.length() > 0){
String ultimo = keywords.substring(keywords.length() - 1);
if (ultimo.equals(" ")){
youtubeSearchView.setText(editable.toString() + "\b");
youtubeSearchView.setSelection(keywords.length());
}
}