リスト ビューの上に編集テキストを実装して、リストを検索しようとしています。
Edit Text を使用した検索はうまく機能せず、彼が思いつく顧客名とそうでない顧客名があります。私を助けてください、以下はコードです
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% 動作させることはできませんでした。私を助けてください。もう聞いてくれてありがとう