csh_34 2012-12-04 07:22 采纳率: 0%
浏览 6319
已采纳

List Filter自定义的Adapter不能得出结果

我正在开发一个应用程序,有一个list列表,在列表上面有一个 edittext编辑框,当我在编辑框中输入内容时,得不到和输入的信息相符合的结果。请问错误出现在哪儿?以下是 Activity和Adapter的代码。谢谢!
Activity

public class ListFilterActivity extends ListActivity{

    ListView list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_filter);   

        list = getListView();
        list.isTextFilterEnabled();

//      final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
//                android.R.layout.simple_list_item_1, android.R.id.text1,
//                getModel());
        final CustomAdapter adapter = new CustomAdapter(ListFilterActivity.this, getModel());
        setListAdapter(adapter);
        EditText filterEditText = (EditText) findViewById(R.id.filterText);
        filterEditText.addTextChangedListener(new TextWatcher() {

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

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

            public void afterTextChanged(Editable s) {
            }
        });
    }

    private ArrayList<String> getModel() {
        ArrayList<String> list = new ArrayList<String>();
        list.add("Linux");
        list.add("Windows7");
        list.add("Suse");
        list.add("Eclipse");
        list.add("Ubuntu");
        list.add("Solaris");
        list.add("Android");
        list.add("Ayes");
        list.add("iPhone");
        return list;
    }

}

** Adapter **

   public class CustomAdapter extends ArrayAdapter<String> {
        private final Context context;
        private final ArrayList<String> names;
        private LayoutInflater mInflater;
        private TextView txt;

        public CustomAdapter(Context context, ArrayList<String> names) {
            super(context, R.layout.row, names);
            this.context = context;
            this.names = names;
            mInflater =  LayoutInflater.from(context);
        }

public Filter getFilter() {     
        if(newFilter == null) {
            newFilter = new Filter() {
                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    // TODO Auto-generated method stub
                    Log.d("TAG", "publishResults");
                    notifyDataSetChanged();
                }
                @Override
                protected FilterResults performFiltering(CharSequence prefix) {             

                    FilterResults results = new FilterResults();
                    ArrayList<String> i = new ArrayList<String>();

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

                        for (int index = 0; index < names.size(); index++) {
                            String si = names.get(index);
                            if(si.compareTo(prefix.toString()) == 0){
                                i.add(si);  
                            }
                        }
                        results.values = i;
                        results.count = i.size();                   
                    }
                    else{
                        synchronized (names){
                            results.values = names;
                            results.count = names.size();
                        }
                    }

                    return results;
                }

            };
        }
            //    Log.d("TAG", "end getFilter");
        return newFilter;
    }
@Override
    public String getItem(int position) {    
        return super.getItem(position);
    }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View rowView  = convertView;
            if(rowView == null) {       
                rowView = mInflater.inflate(R.layout.row, null);
                txt = (TextView)rowView.findViewById(R.id.text1);
            }
            else {
                rowView.getTag();
            }
            txt.setText(names.get(position));

            return rowView;
        }
    }
  • 写回答

4条回答 默认 最新

  • 问答小助手 2012-12-05 02:45
    关注

    你应该在Adapter类中实现Filterable方法,并且重写getFilter()。
    请看这个关于 Filtering custom Adapter事例

      public class ListFilterActivity extends ListActivity {
    
                private List<String> list = new ArrayList<String>();
                List<String> mOriginalValues;
    
                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
    
                    final MyAdapter adapter = new MyAdapter(this, getModel());
                    setListAdapter(adapter);
    
                    EditText filterEditText = (EditText) findViewById(R.id.filterText);
    
                    // Add Text Change Listener to EditText
                    filterEditText.addTextChangedListener(new TextWatcher() {
    
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                            // Call back the Adapter with current character to Filter
                            adapter.getFilter().filter(s.toString());
                        }
    
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count,int after) {
                        }
    
                        @Override
                        public void afterTextChanged(Editable s) {
                        }
                    });
                }
    
                private List<String> getModel() {
                    list.add("Linux");
                    list.add("Windows7");
                    list.add("Suse");
                    list.add("Eclipse");
                    list.add("Ubuntu");
                    list.add("Solaris");
                    list.add("Android");
                    list.add("iPhone");
                    list.add("Windows XP");
                    return list;
                }
    
    // Adapter Class            
    public class MyAdapter extends BaseAdapter implements Filterable {
    
                List<String> arrayList;      
                List<String> mOriginalValues; // Original Values
                LayoutInflater inflater;
    
                public MyAdapter(Context context, List<String> arrayList) {
                    this.arrayList = arrayList;
                    inflater = LayoutInflater.from(context);
                }
    
                @Override
                public int getCount() {
                    return arrayList.size();
                }
    
                @Override
                public Object getItem(int position) {
                    return position;
                }
    
                @Override
                public long getItemId(int position) {
                    return position;
                }
    
                private class ViewHolder {
                    TextView textView;
                }
    
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
    
                    ViewHolder holder = null;
    
                    if (convertView == null) {
    
                        holder = new ViewHolder();
                        convertView = inflater.inflate(R.layout.row, null);
                        holder.textView = (TextView) convertView
                                .findViewById(R.id.textview);
                        convertView.setTag(holder);
                    } else {
                        holder = (ViewHolder) convertView.getTag();
                    }
                    holder.textView.setText(arrayList.get(position));
                    return convertView;
                }
    
                @Override
                public Filter getFilter() {
                    Filter filter = new Filter() {
    
                        @SuppressWarnings("unchecked")
                        @Override
                        protected void publishResults(CharSequence constraint,FilterResults results) {
    
                            arrayList = (List<String>) results.values; // has the filtered values
                            notifyDataSetChanged();  // notifies the data with new filtered values
                        }
    
                        @Override
                        protected FilterResults performFiltering(CharSequence constraint) {
                            FilterResults results = new FilterResults();        // Holds the results of a filtering operation in values
                            List<String> FilteredArrList = new ArrayList<String>();
    
                            if (mOriginalValues == null) {
                                mOriginalValues = new ArrayList<String>(arrayList); // saves the original data in mOriginalValues
                            }
    
                            /********
                             * 
                             *  If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
                             *  else does the Filtering and returns FilteredArrList(Filtered)  
                             *
                             ********/
                            if (constraint == null || constraint.length() == 0) {
    
                                // set the Original result to return  
                                results.count = mOriginalValues.size();
                                results.values = mOriginalValues;
                            } else {
                                constraint = constraint.toString().toLowerCase();
                                for (int i = 0; i < mOriginalValues.size(); i++) {
                                    String data = mOriginalValues.get(i);
                                    if (data.toLowerCase().startsWith(constraint.toString())) {
                                        FilteredArrList.add(data);
                                    }
                                }
                                // set the Filtered result to return
                                results.count = FilteredArrList.size();
                                results.values = FilteredArrList;
                            }
                            return results;
                        }
                    };
                    return filter;
                }
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 Ubuntu20.04无法连接GitHub
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥30 C++行情软件的tick数据如何高效的合成K线