此过滤列表视图的代码正在运行,但是当我删除EditText中的某个字符时,不要返回Listview中的其他字符,当我删除所有caracteres时清理ListView。
这是我的活动:
EditText inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
itensAdapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
这是我的Adapter类
public class ListaItensAdapter extends ArrayAdapter<ItemEstoque> implements Filterable {
private Context context;
private List<ItemEstoque> itens = null;
private List<ItemEstoque> itens_exibicao = null;
Filter f;
public ListaItensAdapter(Context context, List<ItemEstoque> itens) {
super(context, 0, itens);
this.itens = itens;
this.itens_exibicao = itens;
this.context = context;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
ItemEstoque item = itens.get(position);
if (view == null)
view = LayoutInflater.from(context).inflate(R.layout.item_list_estoque, null);
TextView textViewNomeProduto = (TextView) view.findViewById(R.id.text_view_nome);
textViewNomeProduto.setText(item.getNomeProduto());
TextView textViewPreco = (TextView) view.findViewById(R.id.text_view_preco);
textViewPreco.setText(Float.toString(item.getValorUnitarioProduto()));
return view;
}
@Override
public Filter getFilter() {
if(f == null) {
f = new filtrando();
}
return f;
}
private class filtrando extends Filter
{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
FilterResults result = new FilterResults();
if(constraint != null || constraint.toString().length() > 0)
{
ArrayList<ItemEstoque> filteredItems = new ArrayList<ItemEstoque>();
for(int i = 0, l = itens.size(); i < l; i++)
{
ItemEstoque m = itens.get(i);
if(m.getNomeProduto().toLowerCase().contains(constraint.toString())) {
filteredItems.add(m);
}
}
result.count = filteredItems.size();
result.values = filteredItems;
}
else
{
synchronized(this)
{
result.values = itens;
result.count = itens.size();
}
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
itens_exibicao = (ArrayList<ItemEstoque>)results.values;
notifyDataSetChanged();
clear();
for(int i = 0, l = itens_exibicao.size(); i < l; i++)
add(itens_exibicao.get(i));
notifyDataSetChanged();
//notifyDataSetInvalidated();
}
}
我已经看过很多有关此问题的代码,我不知道会发生什么......
答案 0 :(得分:0)
你可以使用简单的过滤方法实现这一点,按照以下给出代码......
EditText inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
itensAdapter.getfilter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
现在您的适配器看起来像这样.........
public class ListaItensAdapter extends ArrayAdapter<ItemEstoque> {
private Context context;
private List<ItemEstoque> itens = null;
private List<ItemEstoque> itens_exibicao = null;
Filter f;
public ListaItensAdapter(Context context, List<ItemEstoque> itens) {
super(context, 0, itens);
this.itens = itens;
this.itens_exibicao = new ArrayList<ItemEstoque>();
this.itens_exibicao.addAll(itens);
this.context = context;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
ItemEstoque item = itens.get(position);
if (view == null)
view = LayoutInflater.from(context).inflate(R.layout.item_list_estoque, null);
TextView textViewNomeProduto = (TextView) view.findViewById(R.id.text_view_nome);
textViewNomeProduto.setText(item.getNomeProduto());
TextView textViewPreco = (TextView) view.findViewById(R.id.text_view_preco);
textViewPreco.setText(Float.toString(item.getValorUnitarioProduto()));
return view;
}
public void getfilter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
itens.clear();
if (charText.length() == 0) {
itens.addAll(itens_exibicao);
}
else
{
for (ItemEstoque m : itens_exibicao)
{
if (m.getNomeProduto().toLowerCase(Locale.getDefault()).contains(charText))
{
itens.add(m);
}
}
}
notifyDataSetChanged();
}
}