Android Custom Adapter Filter not working

不问归期 提交于 2019-12-11 06:57:40

问题


I am using a custom adapter with a search filter in a fragment, however the results don't get filtered on a search, I debugged and stepped through my code and find that it is saying that the args.listenere = null!. What does this mean and how do I correct it? My code is below:

---------Custom Adapter -------

public class SalesPartAdapter extends BaseAdapter implements Filterable {
    private ArrayList<SalesPartItem> listData;
    private ArrayList<SalesPartItem> filteredData;
    private SalesPartFilter filter;
    private Context _context;
    private LayoutInflater layoutInflater;
    public SalesPartAdapter(Context context, ArrayList<SalesPartItem> listData) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(context);
        _context = context;

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

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

    @Override
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        //set background colour

        if (convertView == null) {
            //set up holder
            convertView = layoutInflater.inflate(R.layout.salespart_item, null);
            holder = new ViewHolder();
            holder.salesPartCodeView = (TextView) convertView.findViewById(R.id.salesPartCode);
            holder.salesPartDescView = (TextView) convertView.findViewById(R.id.salesPartDescription);
            holder.salesPartColourBar= (ImageView) convertView.findViewById(R.id.colourBar);
            convertView.setTag(holder);
        } else {
            //use existing holder
            holder = (ViewHolder) convertView.getTag();
        }

        SalesPartItem salespartView = (SalesPartItem) listData.get(position);
        holder.salesPartCodeView.setText(salespartView.SalesPartCode);
        holder.salesPartDescView.setText(salespartView.SalesPartDescription);
        holder.salesPartColourBar.setBackgroundColor(Color.parseColor(salespartView.Colour));//String.valueOf(salespartView.Colour);

        return convertView;
    }

    /********* holder Class to contain previously inflated xml file elements *********/
    static class ViewHolder {
        TextView salesPartCodeView;
        TextView salesPartDescView;
        ImageView salesPartColourBar;
    }

    public Filter getFilter() {
        if (filter == null){
            filter  = new SalesPartFilter();
        }
        return filter;
    }

    ArrayList<SalesPartItem> filteredItems;
    ArrayList<Integer>countFilteredItems;
    private class SalesPartFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            constraint = constraint.toString().toLowerCase();
            FilterResults result = new FilterResults();
            if(constraint != null && constraint.toString().length() > 0)
            {
                filteredItems = new ArrayList<SalesPartItem>();
               // countFilteredItems = new ArrayList<Integer>();
                for(int i = 0, l = listData.size(); i < l; i++)
                {
                    SalesPartItem salesPartItem = listData.get(i);
                    if(salesPartItem.SalesPartDescription.toString().toLowerCase().contains(constraint))
                        filteredItems.add(salesPartItem);
                       // countFilteredItems.add(i);
                }
                result.count = filteredItems.size();
                result.values = filteredItems;
            }
            else
            {
                synchronized(this)
                {
                    result.values = listData;
                    result.count = listData.size();
                }
            }
            return result;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
           // notifyDataSetChanged();
            if (results.count > 0) {
                listData =(ArrayList<SalesPartItem>)results.values;
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }
}

----------------Fragment code that deals with the list view -------------------

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    //USE SAMPLE ID TO GET SALES PARTS LIST FROM DATABASE
    addSalesParts.setEnabled(false);
    //ListViewModel (SalesPartId,salesPartName,NotUsed)
    final int numberOfAddedSalesParts = 3;
    SalesPartItem sp;
    //  fill with some dummy data for now.
    for (long i = 1; i < numberOfAddedSalesParts; i++) {
        sp = new SalesPartItem();
        sp.SalesPartItemId = i;
        sp.SalesPartCode = "LEU" + i;

        if(i!=2){
            sp.SalesPartDescription = "Sales Part " + i;
        }
        else{
            sp.SalesPartDescription = "TSalZes Part " + i;
        }
        sp.Colour = "#cccccc";
        salesPartListViewItems.add(sp);
    }

    // Start of Search filtering
    mListView = (ListView) getActivity().findViewById(R.id.salespartsList);
    //click on item - edit order

    mListView.setTextFilterEnabled(true);


    //display the list via our custom adapter
    mListView.setAdapter(new SalesPartAdapter(getActivity(), salesPartListViewItems);

    EditText searchField = (EditText) getActivity().findViewById(R.id.typeFindSalesPart);
    searchField.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
            //TODO Search Lists from Database and filter accordingly
            SalesPartAdapter SA = new SalesPartAdapter(getActivity(), salesPartListViewItems);
            SA.getFilter().filter(s.toString());
            Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
           // salesPartListViewItems.contains(s);
        }

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

        }
    });

回答1:


I have now resolved this, the issue was that I was creating a new instance of the adapter in the text watcher. Once I set it to use the adapter that created the original list then it worked fine



来源:https://stackoverflow.com/questions/28363382/android-custom-adapter-filter-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!