阵列适配器自定义筛选器不筛选数据

时间:2017-08-04 02:49:28

标签: android filter

我有以下数组适配器,并在我的活动中实现了搜索。但是,当我在搜索字段中键入时,项目不会被过滤。我不知道现在该做什么。

public class SalesTicketsArrayAdapter extends ArrayAdapter<InvoiceTicket> implements Filterable  {
    private ArrayList<InvoiceTicket> mTickets;
    private final InvoiceCustomerDataTable mInvoiceCustomerTable;
    private ValueFilter valueFilter;
    private ArrayList<InvoiceTicket> mStringFilterList;

    Context mContext;

    private static class ViewHolder{
        TextView idView;
        TextView codeView;
        TextView descView;
        TextView customerView;
        TextView dateView;
    }

    private static final int ITEM_VIEW_TYPE_ITEM = 0;
    private static final int ITEM_VIEW_TYPE_SEPARATOR = 1;

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return startOfNewSection(position) ? ITEM_VIEW_TYPE_SEPARATOR : ITEM_VIEW_TYPE_ITEM;
    }

    public SalesTicketsArrayAdapter(@NonNull Context context, @NonNull ArrayList<InvoiceTicket> tickets) {
        super(context, R.layout.row_sales_tickets, tickets);
        mContext = context;
        mTickets = tickets;
        mInvoiceCustomerTable = new InvoiceCustomerDataTable(mContext);
    }

    @NonNull
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        InvoiceTicket ticket = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        ViewHolder viewHolder; // view lookup cache stored in tag
        if (convertView == null) {
            // If there's no view to re-use, inflate a brand new view for row
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.row_sales_tickets, parent, false);
            viewHolder.idView = (TextView) convertView.findViewById(R.id.row_sales_tickets_tv_id);
            viewHolder.codeView = (TextView) convertView.findViewById(R.id.row_sales_tickets_tv_code);
            viewHolder.descView = (TextView) convertView.findViewById(R.id.row_sales_tickets_tv_desc);
            viewHolder.customerView = (TextView) convertView.findViewById(R.id.row_sales_tickets_tv_customer);
            viewHolder.dateView = (TextView) convertView.findViewById(R.id.row_sales_tickets_tv_date);

            // Cache the viewHolder object inside the fresh view
            convertView.setTag(viewHolder);
        } else {
            // View is being recycled, retrieve the viewHolder object from tag
            viewHolder = (ViewHolder) convertView.getTag();
        }
        // Populate the data from the data object via the viewHolder object
        // into the template view.

        if (startOfNewSection(position)) {
            viewHolder.dateView.setText(getFormattedDate(ticket.getDate()));
            viewHolder.dateView.setText(getFormattedDate(ticket.getDate()));
            viewHolder.dateView.setVisibility(View.VISIBLE);
            notifyDataSetChanged();
        }

        viewHolder.idView.setText(String.valueOf(ticket.getId()));
        //viewHolder.codeView.setText("["+ticket.getCode()+"]");
        viewHolder.descView.setText(ticket.getDescription());

        InvoiceCustomer customer = mInvoiceCustomerTable.get(ticket.getInvoiceId());
        viewHolder.customerView.setText((customer.getFullname().equals("") ? ticket.getCode()+" | "+customer.getPhone() : ticket.getCode()+" | "+customer.getFullname()).toLowerCase().toUpperCase());

        // Return the completed view to render on screen
        return convertView;
    }

    @Override
    public Filter getFilter() {
        // TODO Auto-generated method stub
        if(valueFilter == null) {

            valueFilter = new ValueFilter();
        }

        return valueFilter;
    }

    private class ValueFilter extends Filter {

        //Invoked in a worker thread to filter the data according to the constraint.
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            ArrayList<InvoiceTicket> filterList = new ArrayList<InvoiceTicket>();

            if(constraint != null && constraint.length() > 0){
                for(int i=0; i< mStringFilterList.size(); i++)
                {
                    if((mStringFilterList.get(i).getCode().toLowerCase())
                            .contains(constraint.toString().toLowerCase()) || (mStringFilterList.get(i).getDate())
                            .contains(constraint.toString()) ) {
                        InvoiceTicket ticket = new InvoiceTicket(
                                mStringFilterList.get(i).getId(),
                                mStringFilterList.get(i).getInvoiceId(),
                                mStringFilterList.get(i).getCode(),
                                mStringFilterList.get(i).getDescription(),
                                mStringFilterList.get(i).getPrice(),
                                mStringFilterList.get(i).getVenue(),
                                mStringFilterList.get(i).getDate(),
                                mStringFilterList.get(i).getPrintedAt()
                        );
                        filterList.add(ticket);
                    }
                }

                results.count = filterList.size();
                results.values = filterList;
            } else{
                results.count = mStringFilterList.size();
                results.values = mStringFilterList;
            }
            return results;
        }


        //Invoked in the UI thread to publish the filtering results in the user interface.
        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            mTickets = (ArrayList<InvoiceTicket>) results.values;

            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }

}

0 个答案:

没有答案