实时文本过滤

huangapple go评论67阅读模式
英文:

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();
}
----------

huangapple
  • 本文由 发表于 2023年4月19日 22:26:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定