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

泪湿孤枕 提交于 2019-12-04 02:25:43

问题


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

回答1:


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!




回答2:


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



来源:https://stackoverflow.com/questions/29597045/searchview-on-actionbar-how-to-perform-sqlite-search-on-a-custom-listview-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!