Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AutoCompleteTextView with data from a web service, problems displaying the suggestion list

a problem that I can't manage to resolve it.

What I want to achive: Display suggestions in a AutoCompleteTextView comming from a web service call. The final result should look like this:

enter image description here

What I did so far:

In my layout I have an AutoCompleteTextView like this

 <AutoCompleteTextView
            android:id="@+id/txtAutoSearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:selectAllOnFocus="true"
            android:singleLine="true" />

On my Activity I have this:

 autoCompleteAdapter = new AdapterAutoComplete(context, ????? what should I have here?????, null);
         autoCompleteAdapter.setNotifyOnChange(true);
         txtAutoSearch.setAdapter(autoCompleteAdapter);
            txtAutoSearch.addTextChangedListener(new TextWatcher() {

                private boolean shouldAutoComplete = true;

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    shouldAutoComplete = true;

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (shouldAutoComplete) {
                       new GetAutoSuggestionsTask().execute(s.toString());
                    }
                }

            });

The AsyncTask is pretty simple, onPostExecute I set the adapter with the data returned from the webservice

autoCompleteAdapter = new AdapterAutoComplete(context, ????? what should I have here?????,result);
txtAutoSearch.setAdapter(autoCompleteAdapter);

The adapter looks like this:

public class AdapterAutoComplete  extends ArrayAdapter<Person> {

    private Activity activity;
    private List<Person> lstPersons;
    private static LayoutInflater inflater = null;

    public AdapterAutoComplete(Activity a, int textViewResourceId, List<Person> lst) {
        super(a, textViewResourceId, lst);
        activity = a;
        lstPersons = lst;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return lstPersons.size();
    }

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

    public Person getCurrentPerson(int position) {
        return lstPersons.get(position);
    }

    public void removeItem(int position) {
        lstPersons.remove(position);
    }

    public static class ViewHolder {
        public TextView txtName;
        public TextView txtProfession;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        ViewHolder holder;
        if (convertView == null) {
            vi = inflater.inflate(R.layout.people_list_item, null);
            Utils.overrideFonts(activity, vi);

            holder = new ViewHolder();
            holder.txtName = (TextView) vi.findViewById(R.id.txtName);
            holder.txtProfession = (TextView) vi.findViewById(R.id.txtProfession);

            vi.setTag(holder);
        } else {
            holder = (ViewHolder) vi.getTag();
        }

        Person o = lstPersons.get(position);

        if (o.name != null) {
            holder.txtName.setText(o.name);
        } else {
            holder.txtName.setText("N/A");
        }

        if (o.profession != null) {
            holder.txtProfession.setText(o.profession);
        } else {
            holder.txtProfession.setText("N/A");
        }
        return vi;
    }
}

What actual happens:

  • the web service returns the list I need, so data is available
  • in Adapter the getView doesn't seem to execute so I guess there is something wrong done in it.
  • there is no suggestion list displayed with my values
  • I have no idea what I need for Adapter constructor, for textViewResourceId. What am I doing wrong? I am stuck please help.
like image 924
Alin Avatar asked Feb 08 '12 11:02

Alin


People also ask

How do you get the ID of the selected item of the AutoCompleteTextView in Android?

Just call your adapter. getItem(position) and you will get the output you want. For example skillLevelAdapter. getItem(position).

What does Android completion hint attribute in AutoCompleteTextView?

This defines the hint view displayed in the drop down menu. This defines the number of characters that the user must type before completion suggestions are displayed in a drop down menu. This is the View to anchor the auto-complete dropdown to.

How do I set autocomplete text on Android?

If you want to get suggestions , when you type in an editable text field , you can do this via AutoCompleteTextView. It provides suggestions automatically when the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.

What is MultiAutoCompleteTextView?

MultiAutoCompleteTextView is an editable TextView, extending AutoCompleteTextView. In a text view, when the user starts to type a text, MultiAutoCompleteTextView shows completion suggestions for the substring of the text and it is useful for the user to select the option instead of typing.


1 Answers

After a quick scan through of the documentation it looks like your on the right track.

You need to have you async request to get a list of results from an external source. Autocomplete should be rechecked on each key press (cancelling the previous request for data), this should be fired using addTextChangedListener which is what you're doing. So far so good.

From this list of results you need to construct and fill an Adapter: You're custom adapter should extend ListAdapter & Filterable (as described in setAdapter() documentation).

Once you have an adapter it should be as simple as calling:

// AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.countries_list);
textView.setAdapter(adapter);

Remember that any UI changes should be done on the UI Thread or using a Handler.

getView() should be called when the adapter has:

  • Changed with setAdapter()
  • .notifyDataSetChanged() has been called on the ListView.

If the above two events don't cause new getView()s.You should ensure the methods getCount() and getItem() are returning the correct values.

like image 128
Graeme Avatar answered Oct 09 '22 02:10

Graeme