2014-03-12 31 views
1

我是新開發的android應用程序。在我的應用程序中,我有一個列表視圖,它顯示存儲在SQLite中的項目,現在我需要實現一個搜索,它將搜索從SQLite數據庫檢索項目並在列表視圖中顯示。在我的Android應用程序中實現搜索(edittext)的步驟

+0

檢索陣列中的所有數據list.then搜索你想在陣列每次搜索list.don't火查詢源碼搜索 –

+0

在這裏你去https://developer.android.com/training什麼/search/index.html – Egor

+0

感謝提示......因爲我是初學者,如果你能爲我提供一個例子或教程來做,它將會很棒。 – Jaydroid

回答

0

你有Arraylist中的所有數據只是在您的活動中實現TextWatcher並在onTextchanged中調用filterlist方法。

@Override 
public void onTextChanged(CharSequence s, int start, int before, int count) { 
    // TODO Auto-generated method stub 
    adp.filterList(s); 
} 
// add this method in adapter 
    public emotions_adapter(Context context, ArrayList<String> emotions_symbol, 
     ArrayList<String> emotions_name) { 
    emo_symbole = emotions_symbol; 
    emo_name = emotions_name; 
    this.response_name = emotions_name; 
    this.response_symbol = emotions_symbol; 
    C = context; 
    l_Inflater = LayoutInflater.from(context); 

} 

public void filterList(CharSequence s) { 
    if (s.toString().length() > 0) { 
     ArrayList<String> filteredname = new ArrayList<String>(); 
     ArrayList<String> filteredsymbole = new ArrayList<String>(); 

     for (int i = 0; i < response_name.size(); i++) { 

      if (response_name.get(i).toLowerCase().contains(s.toString().toLowerCase())|| response_symbol.get(i).toLowerCase() 
          .contains(s.toString().toLowerCase())) { 

       filteredname.add(response_name.get(i)); 
       filteredsymbole.add(response_symbol.get(i)); 
      } 
     } 

     emo_name = filteredname; 
     emo_symbole = filteredsymbole; 

     notifyDataSetChanged(); 
    } else { 
     emo_name = response_name; 
     emo_symbole = response_symbol; 
     notifyDataSetChanged(); 
    } 
} 
+0

在getview中使用emo_name和emo_symbole更新的數組列表來設置數據。 –

0

那麼u必須得到編輯的文本文字像

Edittext.addTextChangedListener(new TextWatcher() { 

        @Override 
        public void onTextChanged(CharSequence s, int start, 
          int before, int count) { 
         // TODO Auto-generated method stub 
       get text over here 

        @Override 
        public void beforeTextChanged(CharSequence s, int start, 
          int count, int after) { 
         // TODO Auto-generated method stub 

        } 

        @Override 
        public void afterTextChanged(Editable s) { 
         // TODO Auto-generated method stub 

        } 


    }); 

然後搜索文本在您的數據庫,然後你的列表視圖中重新設置適配器,你的問題就解決了 你要清楚數組列表或列表如果你正在使用然後再次添加值,然後更新列表視圖:)

相關問題