2010-08-23 135 views
18

安卓是否有可能爲ListView設置搜索欄,以便在搜索欄被觸摸時會彈出一個鍵盤,當在搜索欄中輸入文本時,會顯示ListView中匹配的項目?android - 搜索listview?

我真正需要的是帶有鍵盤的搜索欄。

更新:

我已經添加,帶來了一個鍵盤,我可以鍵入的EditText字段的EditText場。我想要的是ListView中顯示的列表中項目的前幾個字符與輸入到EditText窗口中的字符匹配。

我試過按照這裏列出的方法ListView Filter,但我有點困惑,有多少過濾已經在ListView中完成?

1)我是否需要創建一個單獨的數組,用於存儲與輸入到EditText中的文本相匹配的值?從這篇文章Call adapter.notifyDataSetChanged看來,ListView已經有了一個陰影數組來做到這一點,並且它在adapter.notifyDataSetChanged()時被更新。叫做。

2)我需要調用adapter.notifyDataSetChanged();在EditText窗口中鍵入一些文本後ListView被更新了嗎?

3)我是否需要擴展ListActivity,因爲此post表示?如果是的話,如果活動類已經從主要活動擴展,我該如何擴展我的活動類?

4)我現在有如下:

ArrayAdapter<String> adapter = null; 
private EditText filterText = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.symptom); 
    ListView symptomList = (ListView) findViewById(R.id.ListView_Symptom); 
    symptomList.setTextFilterEnabled(true); 
    symptomList.setFastScrollEnabled(true); 
    filterText = (EditText) findViewById(R.id.search_box); 
    filterText.addTextChangedListener(filterTextWatcher); 

    adapter = new ArrayAdapter<String>(this, R.layout.menu_item, symptomsArray); 
    symptomList.setAdapter(adapter); 

    private TextWatcher filterTextWatcher = new TextWatcher() { 

     public void afterTextChanged(Editable s) { 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
     } 

     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
      adapter.getFilter().filter(s); 
      adapter.notifyDataSetChanged(); 
     } 

    }; 

不幸的是在那一刻,我在的EditText框中鍵入,我在

Thread [<7> Filter] (Suspended (exception NullPointerException)) 
ArrayAdapter$ArrayFilter.performFiltering(CharSequence) line: 437 
Filter$RequestHandler.handleMessage(Message) line: 234 
Filter$RequestHandler(Handler).dispatchMessage(Message) line: 99  
Looper.loop() line: 123 
HandlerThread.run() line: 60  

任何想法得到一個空指針異常是什麼我我錯過了?

回答

19

你做很小的錯誤: - 創建陣列適配器之前設置的文本改變監聽到編輯文本

看校正後的代碼

public class SearchListView extends Activity 
{ 


/** Called when the activity is first created. */ 

private ListView lv1; 
private String lv_arr[] = 
{ "Android", "iPhone", "BlackBerry", "me", "J2ME", "Listview", "ArrayAdapter", "ListItem", "Us", "UK", "India" }; 
ListView lst; 
EditText edt; 
ArrayAdapter<String> arrad; 



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


    lv1=(ListView)findViewById(R.id.ListView01); 
    edt = (EditText) findViewById(R.id.EditText01); 

    arrad = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr); 
    lv1.setAdapter(arrad); 

    // By using setTextFilterEnabled method in listview we can filter the listview items. 

    lv1.setTextFilterEnabled(true); 
    edt.addTextChangedListener(new TextWatcher() 
    { 


     @Override 
     public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) 
     { 
      // TODO Auto-generated method stub 

     } 



     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) 
     { 
      // TODO Auto-generated method stub 

     } 



     @Override 
     public void afterTextChanged(Editable arg0) 
     { 
      // TODO Auto-generated method stub 
      SearchListView.this.arrad.getFilter().filter(arg0); 

     } 
    }); 

} 

}

4

如果您在您的適配器中實施了FilterableListView can handle the filtering

+0

我遵循你的建議,並用我使用的代碼更新了我的問題。任何想法我的代碼中缺少什麼? – 2010-08-30 02:47:28

+1

Android源代碼中的ArrayAdapter實現了Filterable:http://android.git.kernel.org/?p=platform/frameworks/base。GIT中; A =斑點; F =芯/ JAVA /機器人/插件/ ArrayAdapter.java; H = 32e55048ed18734c71c95b7f9a4e82283a7bf6c1; HB =參/頭/升級Froyo – noah 2010-09-07 19:40:09

1

作爲另一種方法,ListView本身可以通過處理onKeyUp事件來顯示用戶正在輸入的文本(與使用單獨的TextView相對),將ListView滾動到具有輸入文本的項目以及下劃線該ListView中的文本。我使用AsyncTask來實現ListView文本搜索,並提供了一種便捷的類型提前功能。