英文:
Real time text filtering
问题
Currently, my filter searches for one word, but I need to search for texts with more than one word, and I don't know how to do it.
Txtbuscar.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String palabra = charSequence.toString().trim();
// Filter clients based on the text entered in real-time
if (clientesAdapter != null) {
clientesAdapter.getFilter().filter(palabra);
// Reset the Spinner selection
ClienteAfectado.setSelection(0);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
I don't know what I'm missing. I debugged the theme, and according to Android Studio, the variable "text" is capturing all the text, even if they are two words. However, my Spinner is still not populating with the results; it basically says that they don't exist. My idea is that this editText will help me filter the results.
英文:
What happens is that currently my filter searches for one word and I need to search for texts with more than one word and I don't know how to do it.
´´´
Txtbuscar.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//String palabra = Txtbuscar.getText().toString().trim();
String palabra = charSequence.toString().trim();
//Filtra los clientes según el texto que se va ingresando en tiempo real
if (clientesAdapter != null) {
clientesAdapter.getFilter().filter(palabra);
// Resetea la selección del Spinner
ClienteAfectado.setSelection(0);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
´´´
I don't know what I'm missing :s, I debugged the theme and according to andorid studio the variable text is taking all the text even if they are 2 words, but still my spinner doesn't fill me with the results, basically it says that they don't exist.
My idea is that this editText will help me to filter the results.
答案1
得分: 1
以下是代码的中文翻译:
Txtbuscar.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String inputText = charSequence.toString().trim();
String[] keywords = inputText.split("\\s+");
Log.d("TextWatcher", "输入关键词:" + Arrays.toString(keywords));
if (clientesAdapter != null) {
clientesAdapter.getFilter().filterByKeywords(keywords);
ClienteAfectado.setSelection(0);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
## 让我们验证适配器中的filterByKeywords过程 ##
public void filterByKeywords(String[] keywords) {
filteredClientes.clear();
for (Cliente cliente : originalClientes) {
boolean matchesAllKeywords = true;
for (String keyword : keywords) {
if (!cliente.getName().toLowerCase().contains(keyword.toLowerCase())) {
matchesAllKeywords = false;
break;
}
}
if (matchesAllKeywords) {
filteredClientes.add(cliente);
}
}
Log.d("Filter", "筛选后的客户:" + filteredClientes.toString());
notifyDataSetChanged();
}
英文:
Txtbuscar.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String inputText = charSequence.toString().trim();
String[] keywords = inputText.split("\\s+");
Log.d("TextWatcher", "Input keywords: " + Arrays.toString(keywords));
if (clientesAdapter != null) {
clientesAdapter.getFilter().filterByKeywords(keywords);
ClienteAfectado.setSelection(0);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
## let's verify the filtering process within your adapter's filterByKeywords ##
public void filterByKeywords(String[] keywords) {
filteredClientes.clear();
for (Cliente cliente : originalClientes) {
boolean matchesAllKeywords = true;
for (String keyword : keywords) {
if (!cliente.getName().toLowerCase().contains(keyword.toLowerCase())) {
matchesAllKeywords = false;
break;
}
}
if (matchesAllKeywords) {
filteredClientes.add(cliente);
}
}
Log.d("Filter", "Filtered clients: " + filteredClientes.toString());
notifyDataSetChanged();
}
----------
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论