在列表视图中使用编辑文本进行Android搜索无法正常工作

时间:2012-08-29 20:33:16

标签: android list search android-edittext

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

使用“编辑文本”搜索效果不佳,可以考虑一些客户名称而不是其他客户名称。   请帮帮我,下面是代码

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%完成。请帮我。  已经感谢您的聆听了

1 个答案:

答案 0 :(得分:0)

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

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

每当用户输入类型时,请执行以下操作:

一个。清除Secondary ArrayList。

B中。浏览Primary ArrayList并搜索与用户输入内容匹配的名称。

℃。将这些项添加到Secondary ArrayList。

d。使用Secondary ArrayList创建一个新的适配器。

电子。将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,您必须根据需要进行调整。

希望这可以提供帮助。