Search code examples
androidandroid-actionbarsearchview

SearchView on ActionBar: How to perform SQLite search on a custom Listview with custom adapter?


I have a custom ListView with multiple EditTexts and I would like to make the searching function work with only one of the EditTexts.

I was able to implement the SearchView on the ActionBar so far, but I have no idea how to search within my SQLite database using the onQueryTextChange/Submit methods.

I looked ALL over the youtube and google and could not find absolutely nothing explicitly helpful on working with a custom listview. I was able to find some nice examples on working with a simple listview without SQLite data, but I am not sure how to adapt those to my case... I really need help, please!!

If you need any other info, please feel free to ask and I will post right away. Thank you very much!

CODE

View of my customAdapter

public View getView(int position, View view, ViewGroup parent) {
    Cliente cliente = lista.get(position);

    if(view == null)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.single_row_listar_cliente, null);
    }

    TextView txtNome = (TextView) view.findViewById(R.id.tv_cliente_nome);
    txtNome.setText(cliente.getNome());

    TextView txtSobrenome = (TextView) view.findViewById(R.id.tv_cliente_sobrenome);
    txtSobrenome.setText(cliente.getSobrenome());

    //
    Button btn = (Button) view.findViewById(R.id.btn_cliente_opcoes);


    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("working", "k");
            //Mensagem.Msg(ListarCliente, "oi",1000);
        }
    });


    return view;
}

Solution

  • If you use ArrayAdapter, it provides easy method getFilter() which will filter data out in the list according to search text. But to use same functionality with database you need to use CursorAdapter : Documentation

    Which provides method setFilterQueryProvider(filterQueryProvider) Where you need to pass the text and according query in database class. See below tutorial which provides exact implementation of this:

    http://www.mysamplecode.com/2012/07/android-listview-cursoradapter-sqlite.html

    Hope this helped!