【问题标题】:How to add search option in android application?如何在 Android 应用程序中添加搜索选项?
【发布时间】:2011-07-18 09:11:17
【问题描述】:

我正在 android 中开发一个 android 应用程序,我想在其中使用搜索选项,我可以使用它从 Web 服务或数据库中搜索特定数据项?

【问题讨论】:

  • 您能否提供更多信息,说明您希望通过搜索实现什么目标?
  • 我必须在我的应用程序中创建搜索功能数据来自 web 服务但现在我只想在我的应用程序中搜索对话框我无法在我的应用程序中创建搜索对话框所以你能建议我任何解决方案吗
  • @Amandeep singh - 我的建议是将网络服务中的信息解析到 SQLLite 数据库中,然后您可以在其中执行搜索查询,包括全文搜索。请参阅下面的答案。

标签: android search


【解决方案1】:

创建一个名为 list_item.xml 的 XML 文件并将其保存在 res/layout/ 文件夹中。将文件编辑为如下所示:

 ?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000">
</TextView>

ma​​in.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Country" />
<AutoCompleteTextView android:id="@+id/autocomplete_country"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"/>
</LinearLayout>

java

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

  AutoCompleteTextView textView = (AutoCompleteTextView)  findViewById(R.id.autocomplete_country);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES);
textView.setAdapter(adapter);
}

在 HelloAutoComplete 类中,添加字符串数组:

  static final String[] COUNTRIES = new String[] {
   "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",};

【讨论】:

  • 这看起来不错。但这还不足以让你的搜索框(搜索对话框)实现它,对吧?我的意思是搜索对话框是一个内置功能,可能是一个简单的EditText。你能告诉我如何让我的快速搜索框扩展这个AutoCompleteTextView吗?
【解决方案2】:

在您的数据库中搜索

SQLLite 提供了允许全文搜索的内置功能

来自网络服务

就通常通过网络服务进行搜索而言,您只需通过网络服务将搜索词发送到服务器,服务器执行搜索并解析响应。

Lucene

除非您真正询问的是搜索索引,您可能有关键字可以让您快速索引大型数据库或网络服务,因为两者之间的数据可能不会重复。

在这种情况下你可以使用 LUCENE

但是,要让 lucene 与 Android 一起工作,您可能需要查看源代码并删除一些与 RMI 的依赖项。

查看本页底部的解决方案(只需删除 2 个文件,它应该可以编译并与 Android 一起使用)。

如何实现搜索对话框

开发人员指南只为您提供了足够的内容来开始实现搜索对话框的界面。通读示例源代码(发布在下面),了解它如何融入应用程序。

Android 开发者指南

示例代码

【讨论】:

  • 感谢您的建议,但您能告诉我如何在我的应用程序中创建搜索对话框吗?我从这个链接android.ankara-gtug.org/guide/topics/search/search-dialog.html 进行了搜索,但我不明白。
  • @Amandeep singh - 你和@Avi Kumar Manku 不同吗?如果不能,请将此额外信息添加到问题中。实际上,我会要求您提供代码以显示您到目前为止所做的工作,您有哪些错误/异常。
  • 不,我不是 Avi,我应该以同样的方式提问,或者我可以用我的身份向你提问
  • @Amandeep singh - 如果您在堆栈溢出中找不到您遇到的问题的答案,我建议您提出一个新问题。包括源代码和您遇到的具体问题。这是一个示例应用程序,它展示了如何实现和使用搜索功能。 http://sampleandroidapps.com/searchable-dictionary-source-code-download.html
  • 感谢您。我是 android 新手,所以有时我会遇到问题,所以你可以加入我的聊天室,以便我可以更详细地讨论我的问题我的聊天室是 (learn_andrid)。我刚刚创建了这个。
【解决方案3】:

使用带有按钮的 edittext 或带有按钮的自动完成文本视图。

从文件或数据库中为自动完成部分加载数据。获取数据后,将其传递给 Web 服务调用或在本地数据库中搜索相同的数据。

希望我得到了正确的要求并帮助了你。

【讨论】:

  • 这看起来不错。但这还不足以让你的搜索框(搜索对话框)实现它,对吧?我的意思是搜索对话框是一个内置功能,可能是一个简单的 EditText。你能告诉我如何让我的快速搜索框扩展这个 AutoCompleteTextView 吗?
【解决方案4】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
  • 1970-01-01
  • 2012-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多