开发者

How to add search option in android application?

I am developing an an开发者_Go百科droid application in android in which i want to use search option with which i can search particular data item either from web services or from a database ?


Create an XML file named list_item.xml and save it inside the res/layout/ folder. Edit the file to look like this:

 ?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>

main.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);
}

Inside the HelloAutoComplete class, add the string array:

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


Searching inside your database

SQLLite provides built in functionality to allow for full text searching

  • http://bakhtiyor.com/2009/08/sqlite-full-text-search/

From a webservice

As far as searching via a webservice normally you simply send the search terms to the server via your webservice, the server performs the search and you parse the response.

Lucene

Unless what you are REALLY asking about is a search index, where you may have keywords that allows you to quickly index a large database, or a webservice as the data may not be duplicated between the two.

In which case you can use LUCENE

  • http://www.javacodegeeks.com/2010/05/introduction-to-apache-lucene-for-full.html

However to get lucene to work with Android you may need to check out the source and remove a few depenencies with RMI.

See the bottom of this page for the solution (only need to remove 2 files and it should compile and work with Android).

  • http://groups.google.com/group/android-developers/browse_thread/thread/601329551a87e601

How to implement the search dialog box

The developers guide only gives you enough to start implementing the interface to your search dialog box. Read through the example source code (posted below) to see how it fits together into an application.

Android Developers Guide

  • http://developer.android.com/guide/topics/search/search-dialog.html

Example Code

  • http://sampleandroidapps.com/searchable-dictionary-source-code-download.html


Use edittext with a button or auto complete text view with a button.

Load data from a file or database for the auto complete part. Once you obtain the data, pass it to the web-service call or search your local database for the same.

Hope I got the requirement right and helped you.


you can use the search dialog; see the following link: http://developer.android.com/guide/topics/search/search-dialog.html also: http://mobileorchard.com/android-app-development-implementing-search-activities/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜