2013-06-25 63 views
0

任何人都可以指導我如何使用baseadapter在搜索結果數據中更新自定義列表視圖。使用baseadapter更新帶搜索結果數據的自定義視圖列表

我的代碼是

   public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 
       String searchString = editText.getText().toString(); 
       int textLength = searchString.length(); 

       searchResults.clear(); 
       for (int i = 0; i < name1.size(); i++) { 
        String peoplename = name1.get(i).toString(); 
        Log.d(TAG, "onTextChanged:" + peoplename.length()); 
        if (textLength <= peoplename.length()) { 
         Log.d(TAG, "people left: " + peoplename.length()); 
         if (searchString.equalsIgnoreCase(peoplename.substring(
           0, textLength))) { 
          searchResults.add(name1.get(i)); 
          Log.d(TAG, "searchresult: " + searchResults); 
} 

任何幫助表示讚賞..謝謝

+0

對於ListView的搜索指[此鏈接](http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview /) – CRUSADER

+0

不,我已經可以在listView中搜索。我可以在logcat中顯示結果,但無法在列表視圖中更新 – user2511882

+0

efery搜索結果後,必須更新基本適配器源列表。 – Tugrul

回答

0

您應該添加此代碼段

private TextWatcher searchTextWatcher = new TextWatcher() { 
    @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // ignore 
     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
      // ignore 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      Log.d(Constants.TAG, "*** Search value changed: " + s.toString()); 
      adapter.getFilter().filter(s.toString()); 
     } 
    }; 

然後..

@Override 
    public Filter getFilter() { 
     return new Filter() { 
      @SuppressWarnings("unchecked") 
      @Override 
      protected void publishResults(CharSequence constraint, FilterResults results) { 
       Log.d(Constants.TAG, "**** PUBLISHING RESULTS for: " + constraint); 
       myData = (List<MyDataType>) results.values; 
       MyCustomAdapter.this.notifyDataSetChanged(); 
      } 

      @Override 
      protected FilterResults performFiltering(CharSequence constraint) { 
       Log.d(Constants.TAG, "**** PERFORM FILTERING for: " + constraint); 
       List<MyDataType> filteredResults = getFilteredResults(constraint); 

       FilterResults results = new FilterResults(); 
       results.values = filteredResults; 

       return results; 
      } 
     }; 
    } 

這將操縱在適配器吃的數據按文本改變..

referance

希望這是有益

+0

你的意思是這樣嗎? ();}}}} public void onTextChanged(CharSequence s,int start,int before,int count){} String searchString = editText.getText()。toString(); int textLength = searchString.length(); – user2511882

+0

這是簡單的適配器,我只是去thorgh你的代碼閱讀baseadapter ..更新答案 – CRUSADER

0

拿一個AutoCompleteTextView和適配器設置它。

在適配器

然後,

package com.example.sample.Adapters; 

public class LocationAdapters extends ArrayAdapter<String> { 

    private MyFilter mFilter; // my personal filter: this is very important!! 

    ArrayList<Location> mObjects; 
    ArrayList<String> list = new ArrayList<String>(); 
    ArrayList<Location> postCode = new ArrayList<Location>(); 

    public LocationAdapters(Context context, int resource, 
          int textViewResourceId, ArrayList<Location> objects) { 

     super(context, resource, textViewResourceId); 

     mObjects = objects; 

    } 

    @Override 
    public int getCount() { 
     return list.size(); 
    } 

    @Override 
    public String getItem(int position) { 
     try { 
      return list.get(position); 
     } catch (Exception e) { 
      notifyDataSetChanged(); 
      return ""; 
     } 

    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = super.getView(position, convertView, parent); 
     if (position < postCode.size()) { 
      view.setTag(postCode.get(position)); 
     } 

     return view; 
    } 

    @Override 
    public View getDropDownView(int position, View convertView, ViewGroup parent) { 
     View view = super.getDropDownView(position, convertView, parent); 

     if (position < postCode.size()) { 
      view.setTag(postCode.get(position)); 
     } 

     return view; 
    } 

    @Override 
    public Filter getFilter() { 
     if (mFilter == null) { 
      mFilter = new MyFilter(); 
     } 

     return mFilter; 
    } 

    // the trick is here! 
    private class MyFilter extends Filter { 

     ArrayList<String> tempResult = null; 

     // "constraint" is the string written by the user! 
     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 
      FilterResults results = new FilterResults(); 
      // no constraint => nothing to return 

      if ((constraint == null) || (constraint.length() == 0)) { 
       synchronized (tempResult) { 
        tempResult = new ArrayList<String>(); 
        postCode = new ArrayList<Location>(); 
        results.values = tempResult; 
        results.count = tempResult.size(); 
       } 
      } else { 
       String constr = constraint.toString(); 
       tempResult = new ArrayList<String>(); 
       postCode = new ArrayList<Location>(); 
       for (Location location : mObjects) { 
        if (location.postcode.toLowerCase().contains(constr.toLowerCase()) || location.town.toLowerCase().contains(constr.toLowerCase())) { 

         String loc = location.town.toString(); 
         if (loc.contains(",")) { 
          String[] splittedLoc = loc.split(","); 
          tempResult.add(splittedLoc[0] + ", " + location.county); 
          postCode.add(location); 


         } else { 
          tempResult.add(location.town + ", " + location.county); 
          postCode.add(location); 

         } 
        } 
       } 
       results.values = tempResult; 
       results.count = tempResult.size(); 

      } 

      return results; 

     } 

     @SuppressWarnings("unchecked") 
     @Override 
     protected void publishResults(CharSequence constraint, 
             FilterResults results) { 
      list = (ArrayList<String>) results.values; 
      if (results.count > 0) { 
       notifyDataSetChanged(); 
       Log.e("notify", constraint.toString()); 
      } else { 
       notifyDataSetInvalidated(); 
      } 
     } 
    } 
} 
相關問題