【问题标题】:SearchView on ActionBar: How to perform SQLite search on a custom Listview with custom adapter?ActionBar 上的 SearchView:如何使用自定义适配器在自定义 Listview 上执行 SQLite 搜索?
【发布时间】:2015-04-13 02:30:14
【问题描述】:

我有一个带有多个EditTexts 的自定义ListView,我想让搜索功能只使用EditTexts 之一。

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

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

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

代码

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

【问题讨论】:

  • 使用游标适配器见Tutorial
  • Harry,请将其作为答案发布,以便我选择您作为最佳答案。谢谢!

标签: android android-actionbar searchview


【解决方案1】:

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

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

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

希望这有帮助!

【讨论】:

    【解决方案2】:

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

    在任何一种情况下,您都需要编写内容提供程序。如果您使用 SQLite,您应该已经熟悉内容提供程序,但如果没有,您可能应该 Start Here

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-18
      • 2016-01-01
      • 1970-01-01
      相关资源
      最近更新 更多