自定义适配器,listview getFilter方法无法解析

时间:2015-03-25 13:46:37

标签: java android listview filter adapter

即时构建一个Android应用程序atm,它包含一个包含listview信息的页面,信息来自List,而这些项目来自一个包含2个字符串的类。我想使用过滤器来搜索列表中的项目。我使用this来创建适配器和过滤器。

经过一天的搜索,我仍然有同样的问题,我的adapter.getFilter();方法无法解决。

这是我的自定义适配器类,其下面是一个过滤器类

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class CustomAdapter extends BaseAdapter implements Filterable {

    private List<Facility> mListItems;
    private List<Facility> mFilteredListItems;
    private FacilityFilter facilityFilter = new FacilityFilter();
    private LayoutInflater mLayoutInflater;

    public CustomAdapter(Context context, List<Facility> pDataList) {
        super();
        this.mListItems = new ArrayList<>();
        this.mFilteredListItems = new ArrayList<>();
        mListItems.addAll(pDataList);
        mFilteredListItems.addAll(pDataList);
        mLayoutInflater = LayoutInflater.from(context);

    }

    public void resetData() {
        mFilteredListItems = mListItems;
    }


    static class ViewHolder {
        protected TextView customRowText;
        protected TextView mediumRowText;
    }

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

    @Override
    public Object getItem(int position) {
        return mFilteredListItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder viewHolder;

        Facility facility = mFilteredListItems.get(position);

        if (convertView == null) {

            view = mLayoutInflater.inflate(R.layout.custom_row, parent, false);

            viewHolder = new ViewHolder();

            viewHolder.customRowText = (TextView) view.findViewById(R.id.customRowText);
            viewHolder.mediumRowText = (TextView) view.findViewById(R.id.mediumRowText);

            view.setTag(viewHolder);

        } else {

            view = convertView;
            viewHolder = ((ViewHolder) view.getTag());
        }

        viewHolder.customRowText.setText(facility.title);
        viewHolder.mediumRowText.setText(facility.info);

        return convertView;
    }

    @Override
    public Filter getFilter() {

        return facilityFilter;
    }

    public class FacilityFilter extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {


            FilterResults Result = new FilterResults();
            // if constraint is empty return the original names
            if(constraint.length() == 0 ){
                Result.values = mListItems;
                Result.count = mListItems.size();
                return Result;
            }

            String filterString = constraint.toString().toLowerCase();
            Facility filterableString;

            for(int i = 0; i<mListItems.size(); i++){
                filterableString = mListItems.get(i);
                if(filterableString.toString().toLowerCase().contains(filterString)){
                    mFilteredListItems.add(filterableString);
                }
            }
            Result.values = mFilteredListItems;
            Result.count = mFilteredListItems.size();

            return Result;
        }

        @Override
        protected void publishResults(CharSequence constraint,FilterResults results) {

            mFilteredListItems = (ArrayList<Facility>) results.values;
            notifyDataSetChanged();
        }

    }
}

我在我的活动中使用它: btw mFacilities是我的List,包含Facility类中的项目(带有2个字符串的项目)。

facilityAdapter = new CustomAdapter(getBaseContext(), mFacilities);
        facilityList = (ListView) findViewById(R.id.facilitiesList);
        searchInput = (EditText)findViewById(R.id.searchInput);

        facilityList.setAdapter(facilityAdapter);

        searchInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                facilityAdapter.getFilter();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

1 个答案:

答案 0 :(得分:0)

声明facilityAdapter时,您的实例类型错误。 将BaseAdapter facilityAdapter;更改为CustomAdapter facilityAdapter;。它应该可以解决您的问题。