0

I have an AutoCompleteTextView inside a RelativeLayout inside a FrameLayout. I want to populate the completion list using a class declared as follows:

public class AutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
 ...
 public AutoCompleteAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
    data = new ArrayList<String>();
 }
 ...
}

I attached the adapter as follows:

AutoCompleteTextView tv = (AutoCompleteTextView) v.findViewById(R.id.editTextClient);
    AutoCompleteAdapter adapter = new AutoCompleteAdapter(getActivity(), R.layout.fragment_main_right);
    tv.setAdapter(adapter);

where R.layout.fragment_main_right is the enclosing FrameLayout mentioned above. When I start to input text, I get a ClassCastException with the following message: "android.widget.FrameLayout cannot be cast to android.widget.TextView". I understand this to mean that the second parameter in the ArrayAdapter constructor should be the id of something derived from a TextView. All the examples show this parameter as being the enclosing layout. Can someone clear up my confusion?

afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
Aharon Manne
  • 712
  • 3
  • 11
  • 34
  • This should explain the reason for the cast exception http://stackoverflow.com/questions/9280965/arrayadapter-requires-the-resource-id-to-be-a-textview-xml-problems/9282069#9282069 . Also that layout that you pass to the adapter is the row layout and you probably don't want to use the layout containing the actual auto complete widget. – user Nov 05 '13 at 18:02
  • post and read your stacktrace. – njzk2 Nov 05 '13 at 18:12

1 Answers1

0

For the second parameter in your adapter constructor, pass in android.R.layout.simple_dropdown_item_1line or android.R.layout.simple_list_item_1 instead.

The hint is given away that it expects a textview by the name of the parameter: textViewResoureceId.

Jon F Hancock
  • 3,349
  • 3
  • 23
  • 26