2013-01-09 43 views
0

我試圖從CustomListView實現在BaseAdapterFilterable搜索搜索,但它提供了重複搜索數據..如何防止重複數據,而使用BaseAdapter的Android

  @Override 
    public Filter getFilter() { 
     // TODO Auto-generated method stub 
     // Here myList contains Original data 
     return new MyFilter(myList); 
    } 

private class MyFilter extends Filter { 

    List<ListModel> myValues; 

    public MyFilter(List<ListModel> list) { 
     // TODO Auto-generated constructor stub 
     myValues = new ArrayList<ListModel>(list); 
    } 

    @Override 
    protected FilterResults performFiltering(CharSequence constraint) { 
     // TODO Auto-generated method stub 

     FilterResults results = new FilterResults(); 

     List<ListModel> temp = new ArrayList<ListModel>(); 

     if (TextUtils.isEmpty(constraint)) { 

      if (temp.size() > 0) 
       temp.clear(); 
      temp.addAll(myValues); 
     } 

     else { 
      for (ListModel model : myValues) { 

       if (model.getNames().toLowerCase.contains(constraint.toString().toLowerCase())) 
        temp.add(model); 
      } 

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

     return results; 
    } 

     @SuppressWarnings("unchecked") 
     @Override 
    protected void publishResults(CharSequence constraint, 
      FilterResults results) { 
     // TODO Auto-generated method stub 

     if (results != null && results.count > 0) { 

      myList.clear(); 
      myList.addAll((List<ListModel>) results.values); 

     } else 
      myList.addAll(myValues); 

     mAdapter.notifyDataSetChanged(); 

    } 
} 

,我沒有得到在那裏我犯了一個錯誤,搜索完美的作品,但它增加了更多的數據,以我的自定義列表視圖..

想我搜索「印度」,那麼它的廣告ds列表視圖的數據爲「印度」。如果我的列表視圖保持10行的大小,它使20行「印度」

任何幫助將被讚賞。

回答

1

最後我能夠解決自己的問題..

下面是解決我的問題幾件事情..

@Override 
    public Filter getFilter() { 
     // TODO Auto-generated method stub 

     return new MyFilter(); 
    } 

    private class MyFilter extends Filter { 

    @Override 
    protected FilterResults performFiltering(CharSequence constraint) { 
     // TODO Auto-generated method stub 

     FilterResults results = new FilterResults(); 

     List<ListModel> temp = new ArrayList<ListModel>(); 

     if (TextUtils.isEmpty(constraint)) { 

      if (temp.size() > 0) 
       temp.clear(); 
      // I have created new List from old myList before search begins 
      temp.addAll(newList); 
     } 
     else 
     { 
      // newList 
      for (ListModel model : newList) 
      { 
       if(model.getNames().toLowerCase.contains(constraint.toString().toLowerCase())) 
        temp.add(model); 
      } 

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

     return results; 
    } 

    @SuppressWarnings("unchecked") 
    @Override 
    protected void publishResults(CharSequence constraint, 
      FilterResults results) { 
     // TODO Auto-generated method stub 

       myList.clear(); 

     if (results != null && results.count > 0)   
      myList.addAll((List<ListModel>) results.values); 
     else 
      myList.addAll(myList); 

     mAdapter.notifyDataSetChanged(); 

    } 
} //updated code 
+0

你碰巧有完整的代碼?我遇到問題:http://stackoverflow.com/questions/20524417/listview-is-blank-while-using-getfilter-function – Si8

相關問題