0

I was searching on google and stackover could not find the exact solution. My problem is that, I have a ArrayList<String> adapter and it has

  1. Gatewick London England
  2. Ory Paris France
  3. Heathrow London England

If user enters "Lon" into AutoCompleteTextView then I have to display line number 1 and 3. Because these have London string.

I tried this link and i pasted code here but it gives warning on line #57

String prefix = constraint.toString().toLowerCase();

PkmnAdapter

public class PkmnAdapter extends ArrayAdapter<String> {

    private ArrayList<Pkmn> original;
    private ArrayList<Pkmn> fitems;
    private Filter filter;

    public PkmnAdapter(Context context, int textViewResourceId,
            ArrayList<Pkmn> items) {
        super(context, textViewResourceId);
        this.original = new ArrayList<Pkmn>(items);
        this.fitems = new ArrayList<Pkmn>(items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        Pkmn pkmn = fitems.get(position);
        if (pkmn != null) {
            TextView tt = (TextView) v.findViewById(R.id.RlabPName);

            if (tt != null) {
                tt.setText(pkmn.getNAME());
            }
        }
        return v;
    }

    @Override
    public Filter getFilter() {
        if (filter == null)
            filter = new PkmnNameFilter();

        return filter;
    }

    private class PkmnNameFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            String prefix = constraint.toString().toLowerCase();

            if (prefix == null || prefix.length() == 0) {
                ArrayList<Pkmn> list = new ArrayList<Pkmn>(original);
                results.values = list;
                results.count = list.size();
            } else {
                final ArrayList<Pkmn> list = new ArrayList<Pkmn>(original);
                final ArrayList<Pkmn> nlist = new ArrayList<Pkmn>();
                int count = list.size();

                for (int i = 0; i < count; i++) {
                    final Pkmn pkmn = list.get(i);
                    final String value = pkmn.getNAME().toLowerCase();

                    if (value.startsWith(prefix)) {
                        nlist.add(pkmn);
                    }
                }
                results.values = nlist;
                results.count = nlist.size();
            }
            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            fitems = (ArrayList<Pkmn>) results.values;

            clear();
            if (fitems != null) {
                int count = fitems.size();
                for (int i = 0; i < count; i++) {
                    Pkmn pkmn = (Pkmn) fitems.get(i);
                    fitems.add(pkmn);
                }
            }
        }

    }
}

MainActivity.java to put adapter

Pkmn[] item = new Pkmn[4];
item[0] = new Pkmn("Gatewick London England");
item[1] = new Pkmn("Ory Paris France");
item[2] = new Pkmn("Heathrow London England");
item[3] = new Pkmn("Ataturk Istanbul Turkey");

ArrayList<Pkmn> list = new ArrayList<Pkmn>(Arrays.asList(item));
MultiAutoCompleteTextView auto = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
PkmnAdapter adap = new PkmnAdapter(this,android.R.layout.simple_list_item_1, list);
Artem Mostyaev
  • 3,874
  • 10
  • 53
  • 60
Winston
  • 1,800
  • 2
  • 20
  • 30
  • java.lang.NullPointerException at com.getcontacts.testsil.PkmnAdapter$PkmnNameFilter.performFiltering(PkmnAdapter.java:57) – Winston Mar 24 '13 at 13:41

1 Answers1

1

First of all, if you enter "Lon" you should not check if the elements start with "Lon". Probably you need to switch the if statement to:

 if (value.contains(prefix)) {
         nlist.add(pkmn);
 }

Before you perform any filtering in your performFiltering() method check if the constraint is null (Hint: use TextUtils class). If so, then return original data. Therefore you are avoiding NPE. You also need to pay attention to critical points where NPE can be thrown like this one:

 if (prefix == null || prefix.length() == 0) {   }

Cheers,

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148