0

我有一个列表视图和一个用于过滤的编辑文本。我能够在 String[] subItems 中过滤和保存结果,但列表视图没有更新。我在谷歌上搜索,但我没有找到解决方案。

这是代码片段。

OnActivityCreated

    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;
  }

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 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);






    }

过滤器类,它是客户列表视图适配器的一部分

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();
    }
}

非常感谢您的帮助。

4

1 回答 1

1

我按照以下教程解决了这个问题。

http://justcallmebrian.com/?p=149

于 2012-09-24T18:05:08.913 回答