使用baseadapter使用搜索结果数据更新自定义视图列表

时间:2013-06-25 07:44:26

标签: android listview android-listview baseadapter

任何人都可以指导我在使用baseadapter时如何使用搜索结果数据更新自定义列表视图。

我的代码是

                public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                String searchString = editText.getText().toString();
                int textLength = searchString.length();

                searchResults.clear();
                for (int i = 0; i < name1.size(); i++) {
                    String peoplename = name1.get(i).toString();
                    Log.d(TAG, "onTextChanged:" + peoplename.length());
                    if (textLength <= peoplename.length()) {
                        Log.d(TAG, "people left: " + peoplename.length());
                        if (searchString.equalsIgnoreCase(peoplename.substring(
                                0, textLength))) {
                            searchResults.add(name1.get(i));
                            Log.d(TAG, "searchresult: " + searchResults);
}

感谢任何帮助..谢谢

2 个答案:

答案 0 :(得分:0)

您应该添加此代码段

private TextWatcher searchTextWatcher = new TextWatcher() {
    @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // ignore
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // ignore
        }

        @Override
        public void afterTextChanged(Editable s) {
            Log.d(Constants.TAG, "*** Search value changed: " + s.toString());
            adapter.getFilter().filter(s.toString());
        }
    };

然后..

@Override
    public Filter getFilter() {
        return new Filter() {
            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                Log.d(Constants.TAG, "**** PUBLISHING RESULTS for: " + constraint);
                myData = (List<MyDataType>) results.values;
                MyCustomAdapter.this.notifyDataSetChanged();
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                Log.d(Constants.TAG, "**** PERFORM FILTERING for: " + constraint);
                List<MyDataType> filteredResults = getFilteredResults(constraint);

                FilterResults results = new FilterResults();
                results.values = filteredResults;

                return results;
            }
        };
    }

这将根据文本更改操作适配器中的数据..

referance

希望这是有帮助的

答案 1 :(得分:0)

使用AutoCompleteTextView并为其设置适配器。

然后在适配器中,

package com.example.sample.Adapters;

public class LocationAdapters extends ArrayAdapter<String> {

    private MyFilter mFilter; // my personal filter: this is very important!!

    ArrayList<Location> mObjects;
    ArrayList<String> list = new ArrayList<String>();
    ArrayList<Location> postCode = new ArrayList<Location>();

    public LocationAdapters(Context context, int resource,
                            int textViewResourceId, ArrayList<Location> objects) {

        super(context, resource, textViewResourceId);

        mObjects = objects;

    }

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

    @Override
    public String getItem(int position) {
        try {
            return list.get(position);
        } catch (Exception e) {
            notifyDataSetChanged();
            return "";
        }

    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        if (position < postCode.size()) {
            view.setTag(postCode.get(position));
        }

        return view;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View view = super.getDropDownView(position, convertView, parent);

        if (position < postCode.size()) {
            view.setTag(postCode.get(position));
        }

        return view;
    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new MyFilter();
        }

        return mFilter;
    }

    // the trick is here!
    private class MyFilter extends Filter {

        ArrayList<String> tempResult = null;

        // "constraint" is the string written by the user!
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            // no constraint => nothing to return

            if ((constraint == null) || (constraint.length() == 0)) {
                synchronized (tempResult) {
                    tempResult = new ArrayList<String>();
                    postCode = new ArrayList<Location>();
                    results.values = tempResult;
                    results.count = tempResult.size();
                }
            } else {
                String constr = constraint.toString();
                tempResult = new ArrayList<String>();
                postCode = new ArrayList<Location>();
                for (Location location : mObjects) {
                    if (location.postcode.toLowerCase().contains(constr.toLowerCase()) || location.town.toLowerCase().contains(constr.toLowerCase())) {

                        String loc = location.town.toString();
                        if (loc.contains(",")) {
                            String[] splittedLoc = loc.split(",");
                            tempResult.add(splittedLoc[0] + ", " + location.county);
                            postCode.add(location);


                        } else {
                            tempResult.add(location.town + ", " + location.county);
                            postCode.add(location);

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

            }

            return results;

        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            list = (ArrayList<String>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
                Log.e("notify", constraint.toString());
            } else {
                notifyDataSetInvalidated();
            }
        }
    }
}
相关问题