0

我正在尝试在列表视图顶部实现编辑文本以在我的列表中进行搜索。

使用“编辑文本”进行的搜索效果不佳,他可以想到一些客户名称,而其他人则不能。请帮帮我,下面是代码

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% 工作。请帮我。已经感谢收听

4

1 回答 1

0

我在我的应用程序中做了类似的事情,但不是使用适配器进行搜索,而是在 onTextChanged 方法中执行搜索逻辑。

这可以通过根据用户键入的内容创建辅助 ArrayList 来实现,然后使用更新的信息创建辅助适配器。

每当用户键入时,请执行以下操作:

A. 清除辅助 ArrayList。

B. 浏览 Primary ArrayList 并搜索与用户类型匹配的名称。

C. 将这些项目添加到您的辅助 ArrayList。

D. 使用您的辅助 ArrayList 创建一个新适配器。

E. 将您的 ListView 的适配器设置为新的。

    arraycliSort = new ArrayList<Cliente>(); //secondary ArrayList

    yourEditText.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {

        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count){                 

            textlength = yourEditText.getText().length();
            arraycliSort.clear(); //A



            for (int i = 0; i < arraycli.size(); i++) { //B
                if (textlength <= arraycli.get(i).name.length()) {
                    if (yourEditText.getText().toString().equalsIgnoreCase((String) arraycli.get(i).name.subSequence(0,textlength)))
                    {
                        arraycliSort.add(arraycli.get(i)); //C
                    }
                }
            }

            adapter = new ClientAdapter(this, R.id.listView1, arraycli); //D
            yourListView.setAdapter(adapter); //E
        }
    });

在我的示例中,我不确定您如何访问要搜索的名称,因此我使用了 arraycli.get(i).name,您必须根据需要进行调整。

希望这能有所帮助。

于 2012-08-30T02:50:49.103 回答