2012-12-26 79 views
19

我已經在我的SherlockAction Bar的SearchView中實現了搜索過濾器。Android SearchView過濾器列表視圖

當我鍵入m我想在下面的列表視圖中顯示過濾結果,其中只以M開頭等等。但現在它顯示隨機結果。

enter image description here enter image description here

public boolean onQueryTextChange(String newText) { 
    Log.i("Nomad", "onQueryTextChange"); 

    if (TextUtils.isEmpty(newText)) { 
     adapter.getFilter().filter(""); 
     Log.i("Nomad", "onQueryTextChange Empty String"); 
     placesListView.clearTextFilter(); 
    } else { 
     Log.i("Nomad", "onQueryTextChange " + newText.toString()); 
     adapter.getFilter().filter(newText.toString()); 
    } 
    return true; 
} 

public boolean onQueryTextSubmit(String query) { 
    Log.i("Nomad", "onQueryTextSubmit"); 
    return false; 
} 

public boolean onClose() { 
    Log.i("Nomad", "onClose"); 
    return false; 
} 

回答

19

將這個適配器中:

@Override 
public Filter getFilter(){ 
    return new Filter(){ 

     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 
      constraint = constraint.toString().toLowerCase(); 
      FilterResults result = new FilterResults(); 

       if (constraint != null && constraint.toString().length() > 0) { 
        List<String> founded = new ArrayList<String>(); 
         for(YourListItemType item: origData){ 
          if(item.toString().toLowerCase().contains(constraint)){ 
           founded.add(item); 
          } 
        } 

         result.values = founded; 
         result.count = founded.size(); 
        }else { 
         result.values = origData; 
         result.count = origData.size(); 
        } 
      return result; 


    } 
    @Override 
    protected void publishResults(CharSequence constraint, FilterResults results) { 
      clear(); 
      for (String item : (List<String>) results.values) { 
       add(item); 
      } 
    notifyDataSetChanged(); 

    } 

    } 
    } 

而且適配器

public MyAdapter(Context context, int layoutResourceId, String[] places) { 
     super(context, layoutResourceId, data); 
     this.context = context; 

     this.data = Arrays.asList(places); 
     this.origData = new ArrayList<String>(this.data); 

} 
+0

此外,我想確保它過濾的文本只是一個particulat文本。我認爲一些ur代碼丟失或者沒有格式化。你可以請編輯你的答案 –

+0

更好嗎? – Greensy

+0

我只是注意到你正在使用一個數組對象,但我傳遞一個字符串數組到我的適配器。所以我需要做的是將對象轉換成字符串 –

1

的這裏面的構造函數添加元素listview_arr休息碼在......下面: -

listview_arr = new String[listview_array_location.length]; 
    listview_arr = listview_array; 

    setListAdapter(new bsAdapter(this)); 


    et.addTextChangedListener(new TextWatcher() 
    { 
     public void afterTextChanged(Editable s) 
     { 
       // Abstract Method of TextWatcher Interface. 
     } 
     public void beforeTextChanged(CharSequence s, 
       int start, int count, int after) 
     { 
      // Abstract Method of TextWatcher Interface. 
     } 
     public void onTextChanged(CharSequence s,int start, int before, int count) 
     { 
      /*Log.d("count","count==>"+s.length()); 

       if(((s.length()-temp)%4)==0) 
       { 
        Log.d("in if","if in if"+(s.length()-temp)); 
        et.setText(et.getText().toString()+" "); 
        int position = et.getText().toString().length(); 
        Editable etext = et.getText(); 
        Selection.setSelection(etext, position); 
        temp++; 
       }*/ 

      Log.d("count","count==>"+s); 
      textlength = et.getText().length(); 
      array_sort.clear(); 
      for (int i = 0; i < listview_array_location.length; i++) 
      { 
       if (textlength <= listview_array_location[i].length()) 
       { 
        if(et.getText().toString().equalsIgnoreCase((String)listview_array_location[i].subSequence(0,textlength))) 
        { 
         array_sort.add(listview_array[i]); 
        } 
        } 
      } 
      AppendList(array_sort); 
     } 
    }); 
} 

public void AppendList(ArrayList<String> str) 
{ 
    listview_arr = new String[str.size()]; 
    listview_arr = str.toArray(listview_arr); 

    setListAdapter(new bsAdapter(this)); 
} 

public class bsAdapter extends BaseAdapter 
{ 
    Activity cntx; 
    public bsAdapter(Activity context) 
    { 
     // TODO Auto-generated constructor stub 
     this.cntx=context; 

    } 

    public int getCount() 
    { 
     // TODO Auto-generated method stub 
     return listview_arr.length; 
    } 

    public Object getItem(int position) 
    { 
     // TODO Auto-generated method stub 
     return listview_arr[position]; 
    } 

    public long getItemId(int position) 
    { 
     // TODO Auto-generated method stub 
     return listview_array.length; 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) 
    { 
     View row=null; 

     LayoutInflater inflater=cntx.getLayoutInflater(); 
     row=inflater.inflate(R.layout.search_list_item, null); 

     TextView tv=(TextView)row.findViewById(R.id.title); 
     Button Btn01=(Button)row.findViewById(R.id.Btn01); 
     Button Btn02=(Button)row.findViewById(R.id.Btn02); 

     tv.setText(listview_arr[position]); 

     Btn01.setOnClickListener(new OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       Toast.makeText(SearchUser.this, "Button 1 "+listview_arr[position], Toast.LENGTH_SHORT).show(); 
        int color = PreferenceManager.getDefaultSharedPreferences(
          SearchUser.this).getInt(COLOR_PREFERENCE_KEY, Color.WHITE); 
        new ColorPickerDialog(SearchUser.this, SearchUser.this, color).show(); 
      } 
     }); 

     Btn02.setOnClickListener(new OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       Toast.makeText(SearchUser.this, "Button 2 "+listview_arr[position], Toast.LENGTH_SHORT).show(); 
      } 
     }); 

    return row; 
    } 
} 
0

你可以簡單地通過這個example。這是很容易在你的應用程序

這裏整合與陣列和EditText上

5

從適配器獲取過濾器和過濾VY在查詢seachview場改串改邏輯addTextChangedListener

searchView.setOnQueryTextListener(new OnQueryTextListener() { 

     @Override 
     public boolean onQueryTextSubmit(String query) { 
      return false; 
     } 

     @Override 
     public boolean onQueryTextChange(String newText) { 
      getListAdapter().getFilter().filter(newText); 
      return true; 
     } 
    });