我正在尝试在列表视图顶部实现编辑文本以在我的列表中进行搜索。
使用“编辑文本”进行的搜索效果不佳,他可以想到一些客户名称,而其他人则不能。请帮帮我,下面是代码
public class MainActivity extends Activity {
private List<Cliente> listaClientes = null;
public ClienteAdapter adapter;
private ArrayList<Cliente> arraycli = new ArrayList<Cliente>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lista = (ListView) findViewById(R.id.listView1);
arraycli.add(new Cliente("luis", R.drawable.ic_action_search));
arraycli.add(new Cliente("aaaaa", R.drawable.ic_launcher));
arraycli.add(new Cliente("bbbb", R.drawable.ic_launcher));
arraycli.add(new Cliente("ccccc", R.drawable.ic_launcher));
arraycli.add(new Cliente("aaaa", R.drawable.ic_launcher));
arraycli.add(new Cliente("testeeeeee", R.drawable.ic_launcher));
final ClienteAdapter adapter = new ClienteAdapter(this, R.id.listView1, arraycli);
lista.setAdapter(adapter);
EditText procurar = (EditText) findViewById(R.id.edprocurar);
procurar.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString());
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
}
我的自定义适配器
ublic class ClienteAdapter extends ArrayAdapter<Cliente> {
public ArrayList<Cliente> original;
public ArrayList<Cliente> fitems;
private Filter filter;
public ClienteAdapter(Context context, int textViewResourceId,
ArrayList<Cliente> items) {
super(context, textViewResourceId, items);
this.original = new ArrayList<Cliente>(items);
this.fitems = new ArrayList<Cliente>(items);
}
private class ViewHolder {
public TextView ClienteNome;
public ImageView ClienteImg;
}
public void addItem(final Cliente item)
{
this.original.add(item);
}
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
ViewHolder holder;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.clienteitem, null);
holder = new ViewHolder();
holder.ClienteNome = (TextView) v.findViewById(R.id.clinome);
holder.ClienteImg = (ImageView) v.findViewById(R.id.imagem_plista);
v.setTag(holder);
}
else
holder = (ViewHolder)v.getTag();
final Cliente cliente = original.get(position);
if (cliente != null) {
holder.ClienteNome.setText(cliente.getNomeCliente());
holder.ClienteImg.setImageResource(cliente.getImagemCliente());
}
return v;
}
public Filter getFilter()
{
if (filter == null)
filter = new ClassClienteFiltro();
return filter;
}
private class ClassClienteFiltro extends Filter
{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
String prefix = constraint.toString().toLowerCase();
if (prefix == null || prefix.length() == 0)
{
ArrayList<Cliente> list = new ArrayList<Cliente>(original);
results.values = list;
results.count = list.size();
}
else
{
final ArrayList<Cliente> list = new ArrayList<Cliente>(original);
final ArrayList<Cliente> nlist = new ArrayList<Cliente>();
int count = list.size();
for (int i=0; i<count; i++)
{
final Cliente clienteclass = list.get(i);
final String value = clienteclass.getNomeCliente().toLowerCase();
if (value.startsWith(prefix))
{
nlist.add(clienteclass);
}
}
results.values = nlist;
results.count = nlist.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
fitems = (ArrayList<Cliente>)results.values;
clear();
int count = fitems.size();
for (int i=0; i<count; i++)
{
Cliente cliente = (Cliente)fitems.get(i);
add(cliente);
}
}
}
}
我在论坛中看到了很多例子,但遗憾的是无法使其 100% 工作。请帮我。已经感谢收听