Filtering EditText from ListView in android [duplicate]

1

I am making this code that is working blza, but there is a problem, in my string has written "Mr. Jose Ventura" so when I type only "Jose" or just "Ventura" nothing appears in the search, does anyone could I implement this in my code?

public class MainActivity extends ActionBarActivity {
private ListView lv;
private EditText et;
private String[] lst;
private ArrayList<String> lst_Encontrados = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView) findViewById(R.id.lvlist);
    et = (EditText) findViewById(R.id.etlist);

    lst = new String[] {"Sr. Jose Ventura","Sra. Viviana Araujo","Sr. Mario Jorge"};

    //Carrega o listview com todos os itens
    lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lst));
    CarregarEncontrados();

    //Adiciona um TextWatcher ao TextView cujos métodos são chamados sempre
    //que este TextView sofra alterações.
    et.addTextChangedListener(new TextWatcher()
    {
        public void afterTextChanged(Editable s)
        {
            // Abstract Method of TextWatcher Interface.
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
            // Abstract Method of TextWatcher Interface.
        }

        //Evento acionado quando o usuário teclar algo
        //na caixa de texto "Procurar"
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            CarregarEncontrados();

            //Carrega o listview com os itens encontrados
            lv.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, lst_Encontrados));
        }
    });

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {


            AlertDialog.Builder test = new AlertDialog.Builder(MainActivity.this);

            if(((TextView) view).getText().equals("Sr. Jose Ventura")){
                test.setTitle("Doido");
                test.setMessage("doidera");
                test.setNeutralButton("OK", null);
                test.show();


            }
            if(((TextView) view).getText().equals("Sr. Mario Jorge")){
                test.setTitle("Vizinho");
                test.setMessage("kct");
                test.setNeutralButton("OK", null);
                test.show();
            }

            if(((TextView) view).getText().equals("Sra. Viviana Araujo")){
                test.setTitle("eu");
                test.setMessage("louquinha");
                test.setNeutralButton("OK", null);
                test.show();;;
            }


        }
    });



}


public void CarregarEncontrados() {
    int textlength = et.getText().length();

    lst_Encontrados.clear();

    for (int i = 0; i < lst.length; i++) {
        if (textlength <= lst[i].length()) {

            String textoAux = (String) lst[i].subSequence(0, textlength);
            String textoFormatado = et.getText().toString();

            //Removendo acentos do item da lista a comparar
            textoAux = removeAcentos(textoAux);
            //Removendo acentos do item digitado
            textoFormatado = removeAcentos(textoFormatado);
            if (textoFormatado.equalsIgnoreCase(textoAux)) {
                lst_Encontrados.add(lst[i]);

            }
        }
    }
}
private static Map<Character, Character> acentosMap;

public static String removeAcentos(String texto) {

    if (acentosMap == null || acentosMap.size() == 0) {
        acentosMap = new HashMap<>();
        acentosMap.put('À', 'A');
        acentosMap.put('Á', 'A');
        acentosMap.put('Â', 'A');
        acentosMap.put('Ã', 'A');
        acentosMap.put('Ä', 'A');
        acentosMap.put('È', 'E');
        acentosMap.put('É', 'E');
        acentosMap.put('Ê', 'E');
        acentosMap.put('Ë', 'E');
        acentosMap.put('Í', 'I');
        acentosMap.put('Ì', 'I');
        acentosMap.put('Î', 'I');
        acentosMap.put('Ï', 'I');
        acentosMap.put('Ù', 'U');
        acentosMap.put('Ú', 'U');
        acentosMap.put('Û', 'U');
        acentosMap.put('Ü', 'U');
        acentosMap.put('Ò', 'O');
        acentosMap.put('Ó', 'O');
        acentosMap.put('Ô', 'O');
        acentosMap.put('Õ', 'O');
        acentosMap.put('Ö', 'O');
        acentosMap.put('Ñ', 'N');
        acentosMap.put('Ç', 'C');
        acentosMap.put('ª', 'A');
        acentosMap.put('º', 'O');
        acentosMap.put('§', 'S');
        acentosMap.put('³', '3');
        acentosMap.put('²', '2');
        acentosMap.put('¹', '1');
        acentosMap.put('à', 'a');
        acentosMap.put('á', 'a');
        acentosMap.put('â', 'a');
        acentosMap.put('ã', 'a');
        acentosMap.put('ä', 'a');
        acentosMap.put('è', 'e');
        acentosMap.put('é', 'e');
        acentosMap.put('ê', 'e');
        acentosMap.put('ë', 'e');
        acentosMap.put('í', 'i');
        acentosMap.put('ì', 'i');
        acentosMap.put('î', 'i');
        acentosMap.put('ï', 'i');
        acentosMap.put('ù', 'u');
        acentosMap.put('ú', 'u');
        acentosMap.put('û', 'u');
        acentosMap.put('ü', 'u');
        acentosMap.put('ò', 'o');
        acentosMap.put('ó', 'o');
        acentosMap.put('ô', 'o');
        acentosMap.put('õ', 'o');
        acentosMap.put('ö', 'o');
        acentosMap.put('ñ', 'n');
        acentosMap.put('ç', 'c');
    }

    if (texto == null) {
        return "";
    }

    StringBuilder sb = new StringBuilder(texto);

    for (int i = 0; i < texto.length(); i++) {
        Character c = acentosMap.get(sb.charAt(i));
        if (c != null) {
            sb.setCharAt(i, c);
        }
    }

    return sb.toString();

}
    
asked by anonymous 11.08.2015 / 21:11

1 answer

1

In this part of your code:
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lst));
it is better to use a variable and use the getfilter method of the ArrayAdapter, making this change your code would look like this:

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, lst_Encontrados);
 lv.setAdapter(adapter);

And in the addTextChangedListener method you manipulate the results from the adapter and not from the ListView and putting the adapter inline would be impossible to use it like this:

 et.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                                      int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                  int arg3) {
            // TODO Auto-generated method stub
            if (adapter != null) {
                adapter.getFilter().filter(arg0.toString());
            }
            if (arg0.toString().equals("")) {
                adapt.notifyDataSetChanged();
            }
        }

    });

This way you do not need to filter using your created method, using only one function of the ArrayAdapter itself.

    
11.08.2015 / 21:46