0

Hello all i want to add search funcationality to listview for that i have doen following

    @Override
    public Filter getFilter() {
        if (valueFilter == null) {
            valueFilter = new ValueFilter();
        }
        return valueFilter;
    }

    private class ValueFilter extends Filter {

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

            if (constraint != null && constraint.length() > 0) {

                for (int i = 0; i < mStringFilterList.size()-1; i++) {
                    if ((mStringFilterList.get(i).getServiceName().toString())
                            .contains(constraint.toString())) {
                        // Contacts contacts = new Contacts();


                        contacts.setServiceName(mStringFilterList.get(i)
                                .getServiceName());
                        contacts.setAmount(mStringFilterList.get(i).getAmount()
                                + "");
                        System.out
                                .println("mStringFilterList.get(i).getAmount()"
                                        + mStringFilterList.get(i).getAmount());
                        contacts.setTrnDate(mStringFilterList.get(i)
                                .getTrnDate());
                        System.out
                                .println("mStringFilterList.get(i).getTrnDate()"
                                        + mStringFilterList.get(i).getTrnDate());
                        filterList.add(contacts);
                    }
                }
                results.count = filterList.size();
                results.values = filterList;
            } else {
                results.count = mStringFilterList.size();
                results.values = mStringFilterList;
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
             list=(ArrayList<Contacts>) results.values;

            notifyDataSetChanged();
        }

    }

}

But getting error on mStringFilterList.get(i).getServiceName().toString())

2
  • It'll help if you post the code for MyTransactionAdpter Commented Mar 7, 2016 at 10:25
  • Still can't see where the cause might be, since according to the logs the NPE is in MyTransactionAdpter.getCount(MyTransactionAdpter.java:39) @Hiren Patel's answer might be right though, you might just need to modify the getCount() in your adapter. Commented Mar 7, 2016 at 10:55

1 Answer 1

2

Update getCount() method:

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

Reason: You need to return numbers of elements stored in ArrayList.

Done

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.