2012-09-24 5 views
0

필터링을위한 listview 및 edittext가 있습니다. 필자는 String [] subItem에서 결과를 필터링하고 저장할 수 있지만 listview는 업데이트되지 않습니다. Google에서 검색했지만 해결책을 찾지 못했습니다.ListFragment에서 필터링 한 후에 ListView가 업데이트되지 않습니다.

다음은 코드 단편입니다.

public class IconicAdapter extends ArrayAdapter { 

    public String[] allItems; 
    public String[] subItems; 
    //public ArrayList <String> allItems = new ArrayList<String>(); 
    //public ArrayList <String> subItems = new ArrayList<String>(); 

    private TOCFilter filter; 

TextView tx = null; 


IconicAdapter() { 
super(getActivity(), R.layout.toc_list, Constants.TOC); 

this.subItems = new String[Constants.TOC.size()]; 
this.subItems = Constants.TOC.toArray(this.subItems); 
    this.allItems = this.subItems; 
} 

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


@Override 
public int getCount() { 
//return Constants.TOC.size(); 
    return subItems.length; 
    } 

@Override 
public String getItem(int position) { 
    return subItems[position]; 
} 

public View getView(int position, View convertView,ViewGroup parent) { 
     //LayoutInflater inflater= getLayoutInflater(); 
    View row = getActivity().getLayoutInflater().inflate(R.layout.toc_list, parent, false); 
    //View row=inflater.inflate(R.layout.toc_list, parent, false); 
     try 
     { 
      tx=(TextView)row.findViewById(R.id.mainList); 

      tx.setText(Constants.TOC.get(position).toString()); 
      tx.setTextSize(Constants.txtSize); 
      tx.setTextColor(Color.BLACK); 
     if(Constants.UKDebug) 
     { 
      if(position == 0 || position == 1) 
      { 
       Log.d("iconicadapter", Constants.TOC.get(position) + "");  
      } 
     } 


      } 
      catch (Exception e) {Log.d("main","iconadapter"); e.printStackTrace();} 
     return(row); 






    } 
FilterTextWatcher

private TextWatcher filterTextWatcher = new TextWatcher() { 

    public void afterTextChanged(Editable s) { 

    } 

    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 
    } 

    public void onTextChanged(CharSequence s, int start, int before, 
      int count) { 
     adapter.getFilter().filter(s); 
     adapter.notifyDataSetChanged(); 
    } 



}; 

사용자의 ListView 클래스

public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    setRetainInstance(true); 
    setHasOptionsMenu(true); 




    lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    lv.setCacheColorHint(Color.TRANSPARENT); 
    lv.setFastScrollEnabled(true); 

    lv.setDivider(getResources().getDrawable(android.R.color.black)); 
    lv.setDividerHeight(1); 
//lv.setBackgroundColor(Color.parseColor("#f7f9c3")); //#ffddb0 
    lv.setBackgroundDrawable(getResources().getDrawable(R.drawable.merge)); 
    lv.setScrollingCacheEnabled(false); 
    lv.setSmoothScrollbarEnabled(false);  

    adapter = new IconicAdapter(); 

    setListAdapter(adapter); 



    } 

OnCreateView

 @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     View view = inflater.inflate(R.layout.toc, null); 

     filterText = (EditText) view.findViewById(R.id.editTextFilter); 
     filterText.addTextChangedListener(filterTextWatcher); 
     return view; 
    } 

을 OnActivityCreated 고객 목록보기 어댑터의 일부가 당신의 도움이 매우

private class TOCFilter extends Filter{ 

    @Override 
    protected FilterResults performFiltering(CharSequence constraint) { 
     FilterResults results = new FilterResults(); 
     ArrayList<String> i = new ArrayList<String>(); 
     String[] contents; 

      if (constraint!= null && constraint.toString().length() > 0) { 

       for (int index = 0; index < allItems.length; index++) { 
        String si = allItems[index]; 
        if(si.toLowerCase().startsWith(constraint.toString())){ 
        i.add(si); 
        } 
       } 
       contents = new String[i.size()]; 
       i.toArray(contents); 
       results.values = contents; 
       results.count = contents.length;     
      } 
      else{ 
       synchronized (allItems){ 
        results.values = allItems; 
        results.count = allItems.length; 
       } 
      } 
      return results; 
    } 

    @Override 
    protected void publishResults(CharSequence constraint, 
      FilterResults results) { 
     // TODO Auto-generated method stub 
     subItems = (String[]) results.values; 
     notifyDataSetChanged(); 
    } 
} 

감사입니다개

필터 클래스입니다.

답변

관련 문제