如何在android中使用基础适配器时筛选列表项?

时间:2011-12-05 08:03:46

标签: android listview filtering baseadapter

我一直在搜索通过编辑文本在列表视图中搜索项目,虽然我发现了许多类似于我的问题的问题,但是在任何地方都使用游标适配器,其中过滤结果直接从数据库获取或某些数组列表是使用

在我的情况下,我一直使用几个数组为我的列表视图传递文本使用base adapter.as我以前从未做过这样的事情,我无法理解文本观察者的使用。

我调试了代码并发现那个发布结果方法没有被调用,这可能就是为什么列表视图无法获取新值。 我是否必须使用文本更改方法中名称数组中的刷新值再次调用listviewadaptercontacts类。

请帮忙。

public class AllContactsActivity extends ListActivity implements
android.view.View.OnClickListener, OnItemClickListener {

    ListView lv;
    ListViewAdapterContacts lva;
    String[] names, phones, ids, types;


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        LayoutParams params = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        LinearLayout mainLayout = new LinearLayout(this);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        LayoutInflater layoutInflater = getLayoutInflater();
        mainLayout.addView(layoutInflater.inflate(R.layout.allcontacts, null));
        mainLayout.addView(layoutInflater.inflate(R.layout.allbuttons, null));
        this.addContentView(mainLayout, params);

        configureBottomMenu();
        getContacts();

        lv = new ListView(getApplicationContext());
        lv = (ListView) findViewById(android.R.id.list);
        lva = new ListViewAdapterContacts(this, names, types, phones, ids); //passing arrays to class
        lv.setAdapter(lva);
        et = (EditText) findViewById(R.id.searchcontact);
        et.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub
                lva.getFilter().filter(s);

                lva.notifyDataSetInvalidated();
                lva.notifyDataSetChanged();
                lv.setAdapter(lva);
            }

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

                // TODO Auto-generated method stub

            }

            public void afterTextChanged(Editable s) {
                // TODO Auto-generated   method stub
            }
        });

        //some code
    }
}

public class ListViewAdapterContacts extends BaseAdapter implements Filterable {

    private ArrayFilter mFilter;
    private ArrayList < String > mOriginalValues;
    private final Object mLock = new Object();
    private List < String > mObjects;
    List list;

    Activity context;
    String[] names;
    String[] types;
    String[] numbers;
    String[] ids;
    public ListViewAdapterContacts(Activity context, String[] names, String[] types, String[] numbers, String[] ids) {
        // TODO Auto-generated constructor stub

        this.context = context;
        this.names = names;
        this.types = types;
        this.numbers = numbers;
        this.ids = ids;
        Object[] array = names;
        list = Arrays.asList(array);
    }



    public int getCount() {
        // TODO Auto-generated method stub 
        if (names == null) {
            return 0;
        } else {
            return names.length;
        }
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public class viewHolder {
        TextView top;
        TextView bottom;
        TextView downside;
        TextView base;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        viewHolder holder;
        if (convertView == null) {

            LayoutInflater inflator = context.getLayoutInflater();
            convertView = inflator.inflate(R.layout.textviewonly, null);

            holder = new viewHolder();
            holder.top = (TextView) convertView.findViewById(R.id.toptext);
            holder.bottom = (TextView) convertView.findViewById(R.id.bottomtext);
            holder.downside = (TextView) convertView.findViewById(R.id.lowest);
            holder.base = (TextView) convertView.findViewById(R.id.baseid);
            convertView.setTag(holder);
        } else {
            holder = (viewHolder) convertView.getTag();
        }
        holder.top.setText(names[position]);
        holder.bottom.setText(types[position]);
        holder.downside.setText(numbers[position]);
        holder.base.setText(ids[position]);
        return convertView;
    }

    public Filter getFilter() {
        // TODO Auto-generated method stub
        if (mFilter == null) {
            mFilter = new ArrayFilter();
        }
        return mFilter;
    }

    private class ArrayFilter extends Filter {

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

            if (mOriginalValues == null) {
                synchronized(mLock) {
                    //mOriginalValues = new ArrayList<String>(mObjects);

                    mOriginalValues = new ArrayList < String > (list);
                }
            }

            if (prefix == null || prefix.length() == 0) {
                synchronized(mLock) {
                    ArrayList < String > list = new ArrayList < String > (mOriginalValues);
                    results.values = list;
                    results.count = list.size();
                }
            } else {
                String prefixString = prefix.toString().toLowerCase();

                final ArrayList < String > values = mOriginalValues;
                final int count = values.size();

                final ArrayList < String > newValues = new ArrayList < String > (count);

                for (int i = 0; i < count; i++) {
                    final String value = values.get(i);
                    final String valueText = value.toString().toLowerCase();

                    // First match against the whole, non-splitted value
                    if (valueText.startsWith(prefixString)) {
                        newValues.add(value);
                    } else {
                        final String[] words = valueText.split(" ");
                        final int wordCount = words.length;

                        for (int k = 0; k < wordCount; k++) {
                            if (words[k].startsWith(prefixString)) {
                                newValues.add(value);
                                break;
                            }
                        }
                    }
                }

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

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            //noinspection unchecked
            mObjects = (List < String > ) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在此代码中:

et.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub
        lva.getFilter().filter(s);

        lva.notifyDataSetInvalidated();
        lva.notifyDataSetChanged();
        lv.setAdapter(lva);
    }

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

      // TODO Auto-generated method stub

    }

    public void afterTextChanged(Editable s) {

        // TODO Auto-generated  method stub      
    }
});

仅在ontextChanged()方法中使用:

lva.getFilter().filter(s);