Diego Fortes
Diego Fortes

Reputation: 9790

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;
}

Upvotes: 0

Views: 4727

Answers (2)

Harin
Harin

Reputation: 2423

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!

Upvotes: 3

Greg Ennis
Greg Ennis

Reputation: 15379

I can't tell from your question if you are trying to provide the list of suggestions that drop down when user is typing, or if you want to just handle the search by running your query and displaying results in your UI.

In either case you need to write a content provider. You should already be familiar with content providers if you are using SQLite, but if not, you should probably Start Here

If you are trying to populate the list of suggestions, it's fairly easy task to define your content provider by just extending the SearchRecentSuggestionsProvider. There is alot of information on this here

Upvotes: 0

Related Questions