2011-07-18 50 views
3

我正在開發android中的android應用程序,我想使用搜索選項,可以從Web服務或數據庫中搜索特定數據項?如何在android應用程序中添加搜索選項?

+0

你能否提供更多的信息,究竟是什麼,你正在努力實現與搜索? –

+0

我必須使我的應用程序的搜索功能數據來自webservices,但現在我只想在我的應用程序中搜索對話框我無法在我的應用程序中使該搜索對話框,所以你可以建議我任何解決方案 –

+0

@Amandeep singh - 我什麼建議將網絡服務中的信息解析爲SQLLite數據庫,然後在那裏執行包括全文搜索在內的搜索查詢。請參閱下面的答案。 –

回答

1

使用帶按鈕的edittext或使用按鈕自動完成文本視圖。

從自動完成零件的文件或數據庫加載數據。獲取數據後,將其傳遞給Web服務調用或搜索您的本地數據庫。

希望我得到了正確的要求,並幫助你。

+0

這看起來不錯。但是這不足以讓你的搜索框(搜索對話框)實現它,對吧?我的意思是搜索對話框是一個內置功能,可能是一個簡單的EditText。你能告訴我如何讓我的快速搜索框擴展此AutoCompleteTextView? – Harsh

3

創建名爲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> 

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

裏面的HelloAutoComplete類,添加字符串數組:

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

這看起來不錯。但是這不足以讓你的搜索框(搜索對話框)實現它,對吧?我的意思是搜索對話框是一個內置的功能,可能是一個簡單的'EditText'。你能告訴我如何讓我的快速搜索框擴展這個'AutoCompleteTextView'? – Harsh

3

你的數據庫中搜索

SQLLite提供了內置的功能,允許全文搜索

從web服務

只要通過webservice進行搜索,通常只需通過web服務將搜索條件發送到服務器,服務器就會執行搜索並解析響應。

Lucene的

除非你真正問的是一個搜索索引,在那裏你可以有一個關鍵字,可以讓你快速索引大型數據庫或Web服務,因爲數據可能不是之間進行復制二。

在這種情況下,你可以利用Lucene

但是得到的Lucene與Android合作,您可能需要簽出源,並刪除了幾個depenencies與RMI。

查看解決方案的底部(只需要刪除2個文件,它應該可以編譯和使用Android)。

如何實施搜索對話框

開發人員指南只給你足夠的啓動實現該接口到你的搜索對話框。仔細閱讀示例源代碼(在下面發佈),看看它如何融入應用程序。

Android開發者指南

示例代碼

+0

感謝您的建議,但你能告訴我如何使我的應用程序中的搜索對話框,我從這個鏈接http://android.ankara-gtug.org/guide/topics/search/search-dialog.html搜索,但我沒有說明。 –

+0

@Adedeep singh - 你是@Avi Kumar Manku的另一個人嗎?如果不是,請將這些額外信息添加到問題中。實際上,我會要求您提供代碼以顯示您迄今爲止所做的工作,您擁有的錯誤/例外。 –

+0

不,我不是Avi我應該用相同的方式提問還是我可以用我的Id問你 –

相關問題