2014-07-08 83 views
0

嗨,我正在開發有ListView的應用程序。我已經使用自定義適配器和添加過濾option.It工作正常,當我把文本在ssearchBox但問題是,當我開始刪除文本從searchBox它不會帶回項目..它停留在那裏。自定義ListView篩選和回收列表中的舊項目

這是我的代碼...請幫忙。

public class SimpleAdapter extends BaseAdapter implements SectionIndexer{ 

    private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
    private ArrayList<String> mCurrentList; 
    private Context mContext; 
    HolderFilter holderFilter; 

    public SimpleAdapter(ArrayList<String> mCurrentList, Context mContext) { 
     super(); 
     this.mCurrentList = mCurrentList; 
     this.mContext = mContext; 
    } 

    @Override 
    public int getCount() { 
     if (mCurrentList != null) 
      return mCurrentList.size(); 
     else 
      return 0; 
    } 

    @Override 
    public String getItem(int position) { 
     if (mCurrentList != null) 
      return mCurrentList.get(position); 
     else 
      return null; 
    } 

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

    public void onItemSelected(int position) { 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     final int p = position; 

     LayoutInflater inflater = (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.img_row_layout, null); 

     try { 
      TextView textRow = (TextView) convertView.findViewById(R.id.name); 
      textRow.setText(getItem(position)); 
     } catch (Exception e) { 
     } 

     return convertView; 
    } 

    @Override 
    public int getPositionForSection(int section) { 
     for (int j = 0; j < getCount(); j++) { 
      if (section == 0) { 
       // For numeric section 
       for (int k = 0; k <= 9; k++) { 
        String text = null; 
        try { 
         text = mCurrentList.get(j); 
        } catch (Exception e) { 
        } 
        if (text == null) 
         return 0; 
        else if (String.valueOf(text.charAt(0)).toLowerCase().equals(String.valueOf(String.valueOf(k)).toString().toLowerCase())) 
         return j; 
       } 
      } else { 
       String artist = null; 
       try { 
        artist = mCurrentList.get(j); 
       } catch (Exception e) { 
       } 
       if (artist == null) 
        return 0; 
       else if (String.valueOf(artist.charAt(0)).toLowerCase().equals(String.valueOf(mSections.charAt(section)).toString().toLowerCase())) { 
        return j; 
       } 
      } 
     } 
     return 0; 
    } 

    @Override 
    public int getSectionForPosition(int position) { 
     return 0; 
    } 

    @Override 
    public Object[] getSections() { 
     String[] sections = new String[mSections.length()]; 
     for (int i = 0; i < mSections.length(); i++) 
      sections[i] = String.valueOf(mSections.charAt(i)); 
     return sections; 
    } 
    public Filter getFilter() { 
     if (holderFilter == null){ 
      holderFilter = new HolderFilter(); 
     } 
     return holderFilter; 
    } 
    @SuppressLint("DefaultLocale") 
    private class HolderFilter extends Filter { 
     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 
      FilterResults results = new FilterResults(); 
      if (constraint == null || constraint.length() == 0) { 
       results.values = mCurrentList; 
       results.count = mCurrentList.size(); 
      }else { 
       List<String> nHolderList = new ArrayList<String>(); 
       for (String h : mCurrentList) { 
        if (h.toString().toUpperCase().startsWith(constraint.toString().toUpperCase())) 
         nHolderList.add(h); 
       } 
       results.values = nHolderList; 
       results.count = nHolderList.size(); 
      } 
      return results; 
     } 
     @SuppressWarnings("unchecked") 
     @Override 
     protected void publishResults(CharSequence constraint,FilterResults results) { 
      if (results.count == 0) 
       notifyDataSetInvalidated(); 
      else { 
       mCurrentList = (ArrayList<String>) results.values; 
       notifyDataSetChanged(); 
      } 
     } 
    } 
} 

回答

1

當您過濾項目時,您將重置包含所有項目的原始數組。你需要保留它,通過有兩個數組:

  • 一個將包含所有項目 - 這將用作您的過濾器的基礎。
  • 其次將包含過濾的項目 - 這將實際用於您的適配器。如果沒有結果,您可以簡單地將其設置爲第一個數組。

見下面的例子:

/* 
* Author: Madhur Ahuja 
* Copyright (c) 2014. 
*/ 

package in.co.madhur.vocabbuilder.ui; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.Filter; 
import android.widget.Filterable; 
import android.widget.RatingBar; 
import android.widget.TextView; 

import java.util.ArrayList; 
import java.util.List; 

import in.co.madhur.vocabbuilder.R; 
import in.co.madhur.vocabbuilder.model.Word; 

/** 
* Created by madhur on 22-Jun-14. 
*/ 
public class WordTokenAdapter extends BaseAdapter implements Filterable 
{ 
    private List<Word> words, originalWords; 
    private Context context; 

    public WordTokenAdapter(List<Word> words, Context context) 
    { 
     this.setWords(words); 
     this.context = context; 

     originalWords = new ArrayList<Word>(); 

     for (Word word : words) 
     { 
      originalWords.add(word); 
     } 
    } 

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

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

    @Override 
    public long getItemId(int position) 
    { 
     return getWords().get(position).getId(); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     Word word= (Word) getItem(position); 
     View view; 
     ViewHolder holder; 
     if (convertView == null) 
     { 
      holder = new ViewHolder(); 

      view = LayoutInflater.from(context).inflate(R.layout.word_token_item, parent, false); 
      holder.word = (TextView) view.findViewById(R.id.word); 
      holder.ratingBar=(RatingBar)view.findViewById(R.id.ratingBar); 
      view.setTag(holder); 


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

     } 

     holder.word.setText(word.getName()); 
     holder.ratingBar.setRating((float)word.getRating()/2); 

     return view; 
    } 

    public List<Word> getWords() 
    { 
     return words; 
    } 

    public void setWords(List<Word> words) 
    { 
     this.words = words; 
    } 

    @Override 
    public Filter getFilter() 
    { 
     return new Filter() 
     { 
      @Override 
      protected FilterResults performFiltering(CharSequence constraint) 
      { 

       FilterResults results = new FilterResults(); 


       List<Word> filteredWords; 

       // perform your search here using the searchConstraint String. 

       if (constraint != null && constraint.length() > 0) 
       { 
        filteredWords = new ArrayList<Word>(); 

        for (Word word : originalWords) 
        { 
         if (word.getName().startsWith((String) constraint)) 
         { 
          filteredWords.add(word); 

         } 
        } 
       } 
       else 
       { 
        filteredWords = originalWords; 
       } 


       synchronized (this) 
       { 
        results.count = filteredWords.size(); 
        results.values = filteredWords; 
       } 

       return results; 


      } 

      @Override 
      protected void publishResults(CharSequence constraint, FilterResults results) 
      { 

       List<Word> filteredWords = (List<Word>) results.values; 

       words = filteredWords; 

       notifyDataSetChanged(); 

      } 
     }; 
    } 


    private static class ViewHolder 
    { 

     TextView word; 
     RatingBar ratingBar; 
    } 
} 
+0

對不起,我是新來android.Can你告訴我,在我的片段添加代碼? –