2015-12-29 68 views
0

我實施了可過濾界面。 代碼爲我的適配器該列表未對原始數據執行過濾器

public class LyricListAdapter extends BaseAdapter implements Filterable { 
    Context context; 
    List<Lyric> lyrics; 

    LyricsFilter filter; 

    public LyricListAdapter(Context context, List<Lyric> lyrics) { 
     this.context = context; 
     this.lyrics = lyrics; 

     getFilter(); 
    } 

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

    @Override 
    public Object getItem(int position) { 
     return lyrics.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    //View holder 
    class ViewHolder { 
     ImageView imgThumb; 
     TextView lblTitle; 
     TextView lblArtist; 

    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 

     if(convertView == null) { 
      convertView = inflater.inflate(R.layout.row_lyric, null); 
      holder = new ViewHolder(); 
      holder.imgThumb = (ImageView) convertView.findViewById(R.id.imgThumb); 
      holder.lblTitle = (TextView) convertView.findViewById(R.id.lblTitle); 
      holder.lblArtist = (TextView) convertView.findViewById(R.id.lblArtist); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     try { 
      Lyric lyric = (Lyric) getItem(position); 

      holder.imgThumb.setImageResource(lyric.getThumb()); 
      holder.lblTitle.setText(lyric.getLyricTitle()); 
      holder.lblArtist.setText(lyric.getLyricArtist()); 
     } catch (Exception ex){ 
      ex.printStackTrace(); 
     } 

     return convertView; 
    } 

    @Override 
    public Filter getFilter() { 
     if(filter == null){ 
      filter = new LyricsFilter(); 
     } 
     return filter; 
    } 

    private class LyricsFilter extends Filter { 

     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 
      FilterResults results = new FilterResults(); 

      if(constraint == null || constraint.length() == 0){ 
       results.values = lyrics; 
       results.count = lyrics.size(); 
      } else { 
       ArrayList<Lyric> filteredLyrics = new ArrayList<Lyric>(); 
       for(Lyric l : lyrics){ 
        Log.d("!!!!!!!!!!!", l.getLyricTitle()); 
        if(l.getLyricTitle().toLowerCase().contains(constraint.toString().toLowerCase())) 
        filteredLyrics.add(l); 
       } 

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



      return results; 
     } 

     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 
      lyrics = (ArrayList<Lyric>) results.values; 
      notifyDataSetChanged(); 
     } 
    } 
} 
  • 問題是,當我在編輯文本中的列表中得到 過濾器。
  • 當我鍵入下一個字符時,它從篩選的 結果中篩選出的結果不是原始列表。
  • ,甚至當我刪除編輯文本文本不顯示歌詞

的完整列表過濾通過的另一項活動叫這樣的:

public void onTextChanged(CharSequence s, int start, int before, int count) { 
    Toast.makeText(LyricsListActivity.this, s, Toast.LENGTH_SHORT).show(); 
    Log.d("12123123", s.toString()); 
    adapter.getFilter().filter(s); 
} 
+0

看看[這裏](http://stackoverflow.com/questions/33023111/my-custom-list-filter-is-not-working) – Blackbelt

回答

0

其工作在我的案件罰款

private class ValueFilter extends Filter 
{ 

    //Invoked in a worker thread to filter the data according to the constraint. 
    @Override 
    protected FilterResults performFiltering(CharSequence constraint) 
    { 
     FilterResults results; 
     try 
     { 
      nearbyList = FilterList; 
      results = new FilterResults(); 
      if (constraint != null && constraint.length() > 0) 
      { 
       ArrayList<Card> filterList = new ArrayList<Card>(); 
       String SearchText = constraint.toString().toUpperCase(); 
       for (int i = 0; i < nearbyList.size(); i++) 
       { 
        if ((nearbyList.get(i).name.toUpperCase()).contains(SearchText))// || (nearbyList.get(i).last_name.toUpperCase()).contains(SearchText) || (nearbyList.get(i).company_name.toUpperCase()).contains(SearchText)) 
        { 
         Card card = new Card(); 

         card.user_id = (nearbyList.get(i).user_id); 

         card.name = (nearbyList.get(i).name); 
         card.status = (nearbyList.get(i).status); 
         card.profile_image = (nearbyList.get(i).profile_image); 
         card.company_name = (nearbyList.get(i).company_name); 
         card.designation = (nearbyList.get(i).designation); 

         filterList.add(card); 
        } 
       } 
       results.count = filterList.size(); 
       results.values = filterList; 
      } 
      else 
      { 
       results.count = nearbyList.size(); 
       results.values = nearbyList; 
      } 
      return results; 
     } 
     catch (Exception e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    //Invoked in the UI thread to publish the filtering results in the user interface. 
    @SuppressWarnings("unchecked") 
    @Override 
    protected void publishResults(CharSequence constraint, FilterResults results) 
    { 
     try 
     { 
      nearbyList = (ArrayList<Card>) results.values; 
      notifyDataSetChanged(); 
     } 
     catch (Exception e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

nearbyList = FilterList;此行是爲過濾器給主適配器的數據,以便通過每次路過主適配器的數據,你可以做到這一點。