0

What is the wrong in my code?

below is the custom adapter for AutoCompleteTextView, I override basic methods, and also override the filter method, but it do not give me the desired result.

    public class AirportCodesAdapter extends ArrayAdapter<AirportCodes> {

    public Context mContext;
    public ArrayList<AirportCodes> arrayList;
    public int layoutResourceId;

    public AirportCodesAdapter(Context mContext, int layoutResourceId, ArrayList<AirportCodes> arrayList) {

        super(mContext, layoutResourceId, arrayList);

        this.layoutResourceId = layoutResourceId;
        this.mContext = mContext;
        this.arrayList = arrayList;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        try {
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row_airports_codes, parent, false);
            }
            TextView Code = (TextView) convertView.findViewById(R.id.code_text_list_row_airport_codes);
            TextView airport = (TextView) convertView.findViewById(R.id.airport_name_list_row_airport_codes);

            Code.setText(arrayList.get(position).getCode());
            airport.setText(arrayList.get(position).getAirport());
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    @Override
    public AirportCodes getItem(int position) {

        return arrayList.get(position);
    }

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

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


    @Override
    public Filter getFilter() {
        return nameFilter;
    }


    Filter nameFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults filterResults = new FilterResults();
            ArrayList<AirportCodes> tempList = new ArrayList<AirportCodes>();
            //constraint is the result from text you want to filter against.
            //objects is your data set you will filter from
            if (constraint != null && arrayList != null) {
                int length = arrayList.size();
                int i = 0;
                while (i < length) {
                    for (AirportCodes customer : arrayList) {
                        if(customer.getCode().toString().trim().toUpperCase().startsWith(constraint.toString().trim().toUpperCase()) {
                            tempList.add(customer);
                        }
                    }

//                    AirportCodes item = arrayList.get(i);
//                    //do whatever you wanna do herem
//                    //adding result set output array
//                    tempList.add(item);
                    i++;
                }
                //following two lines is very important
                //as publish result can only take FilterResults objects
                filterResults.values = tempList;
                filterResults.count = tempList.size();
            }
            return filterResults;

        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            arrayList = (ArrayList<AirportCodes>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
//
//
//            if (results != null && results.count > 0) {
//                suggestions = (ArrayList<AirportCodes>) results.values;
//                notifyDataSetChanged();
//            } else {
//                notifyDataSetInvalidated();
//            }

        }

    };
}

I have spent more than 6 hours trying to find why the code shows only list of airports and filter do not filter the results.

EDIT: the desired behavior is to display only the texts that have (startsWith) the typed text in AutoCompleteTextView so I can send it to the previous activity. and I editied my code to be more appropriate I hope my question is clear .

KhalodaRK84
  • 85
  • 2
  • 14
  • Syntax seems to be correct but can you rename the string with something else as `chocolate` and explain what the desired behaviour is supposed to be because saying "it doesn't work" doesn't help us a lot... – Laurent Meyer Nov 22 '15 at 13:33
  • I edited my code and question. – KhalodaRK84 Nov 22 '15 at 13:40
  • IMHO, you can refer to my answer at the following question http://stackoverflow.com/questions/33047156/how-to-create-custom-baseadapter-for-autocompletetextview/33049491#33049491 for more information. Hope this helps! – BNK Nov 23 '15 at 06:01
  • @BNK Thank you, your answer is good :D – KhalodaRK84 Dec 20 '19 at 10:55

0 Answers0