p. ld
p. ld

Reputation: 595

Incompatible type in Android AutoCompleteTextView

I'm trying to add multiple values in AutoCompleteTextView from SQlite Database in Android.But it shows incompatible type for this method =

@Override
public String getItem(int position) {
    return fullList.get(position);
} 

public class AutoCompleteAdapter extends ArrayAdapter<AutoCompleteObject> implements Filterable {

    private ArrayList<AutoCompleteObject> fullList;
    private ArrayList<AutoCompleteObject> mOriginalValues;
    private ArrayFilter mFilter;

    public AutoCompleteAdapter(Context context, int resource, int textViewResourceId, ArrayList<AutoCompleteObject> fullList) {

            super(context, resource, textViewResourceId, fullList);
            this.fullList = fullList;
            mOriginalValues = new ArrayList<AutoCompleteObject>(fullList);

        }

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

        @Override
        public String getItem(int position) {
            return fullList.get(position);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return super.getView(position, convertView, parent);
        }

        @Override
        public Filter getFilter() {
            if (mFilter == null) {
                mFilter = new ArrayFilter();
            }
            return mFilter;
        }

        private class ArrayFilter extends Filter {
            private Object lock;

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

                if (mOriginalValues == null) {
                    synchronized (lock) {
                        mOriginalValues = new ArrayList<AutoCompleteObject>(fullList);
                    }
                }

                if (prefix == null || prefix.length() == 0) {
                    synchronized (lock) {
                        ArrayList<AutoCompleteObject> list = new ArrayList<AutoCompleteObject>(
                                mOriginalValues);
                        results.values = list;
                        results.count = list.size();
                    }
                } else {
                    final String prefixString = prefix.toString().toLowerCase();

                    ArrayList<AutoCompleteObject> values = mOriginalValues;
                    int count = values.size();

                    ArrayList<AutoCompleteObject> newValues = new ArrayList<AutoCompleteObject>(count);

                    for (int i = 0; i < count; i++) {
                        String item = values.get(i);
                        if (item.toLowerCase().contains(prefixString)) {
                            newValues.add(item);
                        }

                    }
                    results.values = newValues;
                    results.count = newValues.size();
                }

                return results;
            }

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint,
                                          Filter.FilterResults results) {

                if (results.values != null) {
                    fullList = (ArrayList<AutoCompleteObject>) results.values;
                } else {
                    fullList = new ArrayList<AutoCompleteObject>();
                }
                if (results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }
        }
    }

Upvotes: 0

Views: 74

Answers (2)

RexSplode
RexSplode

Reputation: 1505

How about this approach?

@Override
    public String getItem(int position) {
        AutoCompleteObject obj = fullList.get(position);
        String item = obj.getYourValue() + " " + obj.getAnotherValue();
        return item;
    }

Of course, it means that you need to change generic type to String.

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157437

your getItem is returning a String, but fullList contains AutoCompleteObject. You should change it like

    @Override
    public AutoCompleteObject getItem(int position) {
        return fullList.get(position);
    }

your Adapter has generic type AutoCompleteObject (ArrayAdapter<AutoCompleteObject>). Also the super.getItem returns T, so you can't really change the signature of the method to make it return a String. If you do so you will get another compile time error

Upvotes: 0

Related Questions