如何在自定义对象的ArrayList中搜索,然后使用结果更新ListView?

时间:2015-06-01 16:19:52

标签: java android listview search arraylist

我想从swfobject ArrayList进行搜索,并从这些对象填充custom objects,但我真的不知道从哪里开始......

这就是我所拥有的。

我从ListViewEditText获得搜索,我知道何时开始寻找:

EditorActionListener

以下是edittext.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) { //I got this from this site, another question int searchListLength = medicos.size(); for (int i = 0; i < searchListLength; i++) { if (medicos.get(i).getNombre().contains(edittext.getText().toString()))) { //This is where Im suposed to do something but I dont know what to use to fill the ListView } } return false; } });

的结构
custom object

依旧.....

谢谢!

更新(更新适配器的代码):

public class Medico  implements Serializable{
    private static final long serialVersionUID = 1L;    
    String nombre, especialidad1,especialidad2,especialidad3,celular,mail,telefono1,telefono2,ext,hospital,direccion;
    double latitud,longitud;
    public Medico(){}
    public Medico(String nombre, String esp1,String esp2, String esp3, String cel,String mail,String tel1,String tel2, String ext,String hospital, String direccion, double lat, double lon){
        this.nombre=nombre;  //and so on.....    
    }

    public void setNombre(String s){
        this.nombre=s;
    }
    public void setEspecialidad1(String s){
        this.especialidad1=s;
    } 

    public String getNombre(){
        return this.nombre;
    }
    public String getEspecialidad1(){
        return this.especialidad1;
    }
}    

2 个答案:

答案 0 :(得分:0)

您需要更新用于填充ListView的适配器。

edittext.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {

            //I will store the matches on this list.
            List<Medico> resultado = new ArrayList<Medico>();
            int searchListLength = medicos.size();
            for (int i = 0; i < searchListLength; i++) {
            if (medicos.get(i).getNombre().contains(searchsds)) {
                // I found a match, I will add it to results
                resultado.add(medicos.get(i));
                }
            }
            //At the end, update the Adapter. I will assume that you have something like this.
            myAdapter.setValues(resultado);
            //You must write the code to set the values on your adapter. If you could post the way you are populating the list view I could help you out with more information.
return false;
        }
    });

答案 1 :(得分:0)

您可以拥有一些resultListArrayList)并提供adapter(例如ArrayAdapter,或者可以是一些自定义适配器ArrayAdapter或{ {1}})BaseAdapter ListView

比方说,您有resultList CustomArrayAdapter

mAdapter

在您lv.setAdapter(mAdapter); 的{​​{1}}内,您可以设置小部件的值,例如来自getView的{​​{1}}:

adapter

当然有捕获量,但它应该有效!看看它是否有帮助!