0

So I have an AutoCompleteTextview which works fine, but I would like to know how to enforce a specific option. The meaning is to use something like setText(), but setText is not good for me, because it doesn't activate the onItemClick event, and this is what I want to activate.

myAutoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parentView, View selectedItemView,                                                   ()int position, long id) {
               // do something...
   }
});

Thanks for any help.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
Eliran
  • 59
  • 1
  • 8

1 Answers1

3

You need to declare the listener as a member variable and force call it from outside, like this:

void onCreate(Bundle savedInstanceState){
     // setContentView and other stuff
     myAutoCompleteTextView.setOnItemClickListener(mOnItemListener);
}


AdapterView.OnItemClickListener mOnItemListener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parentView, View selectedItemView,int position, long id) {
               // do something...
   }
};

and when you need to force call it, use:

mOnItemListener.onItemClick(/*put your forced parameters here*/);

for example:

mOnItemListener.onItemClick(null, null, 10, 20);
Kasra
  • 3,045
  • 3
  • 17
  • 34
  • I understood what you wrote except the last row. Lets say I want to force the option "abc" to get selected. What are the arguments I need to specify? I've read that http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html and it didn't help. – Eliran Feb 19 '15 at 09:16
  • You need to pass the position of row – Kasra Feb 20 '15 at 00:45
  • 1
    I passed the position by the 2 last arguments and it didn't work. Maybe the first 2 arguments are the problematic ones? null,null... – Eliran Feb 20 '15 at 10:13