ActionBar 上的 SearchView:如何使用自定义适配器在自定义 Listview 上执行 SQLite 搜索?

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

我有一个带有多个 EditText 的自定义 ListView,我想让搜索功能仅与一个 EditText 一起使用。

到目前为止,我能够在 ActionBar 上实现 SearchView,但我不知道如何使用 onQueryTextChange/Submit 方法在我的 SQLite 数据库中进行搜索。

我查看了整个 youtube 和 google,但找不到任何对使用自定义列表视图有明确帮助的内容。我能够找到一些关于使用没有 SQLite 数据的简单列表视图的很好的例子,但我不确定如何使它们适应我的情况......我真的需要帮助!!

如果您需要任何 其他信息,请随时询问,我会立即post。非常感谢!

代码

我的自定义适配器视图

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

我无法从你的问题中判断出你是想提供用户输入时下拉的建议列表,还是只想通过 运行 你的查询和显示来处理搜索结果是 UI.

无论哪种情况,您都需要编写内容提供程序。如果您正在使用 SQLite,您应该已经熟悉内容提供程序,但如果不是,您应该 Start Here

如果您尝试填充建议列表,只需扩展 SearchRecentSuggestionsProvider 即可轻松定义您的内容提供程序。关于这个 here

有很多信息

如果您使用 ArrayAdapter,它提供了简单的方法 getFilter(),它会根据搜索文本过滤掉列表中的数据。 但是要对数据库使用相同的功能,您需要使用 CursorAdapterDocumentation

其中提供方法setFilterQueryProvider(filterQueryProvider) 您需要在数据库 class 中传递文本和相应查询的位置。 请参阅下面的教程,其中提供了此的确切实现:

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

希望对您有所帮助!