Why the constructor ArrayAdapter <String> (new View.OnKeyListener () {}, int, String []) is undefined

Why the constructor of ArrayAdapter (new View.OnKeyListener () {}, int, String []) is undefined according to my coding. This encoding is designed to extract data from SQLite when entering a word count of more than 3 characters. But the following error is displayed.

ArrayAdapter Constructor (new View.OnKeyListener () {}, int, String []) - undefined

ed1 = (AutoCompleteTextView)findViewById(R.id.searchWord); ed1.setOnKeyListener(new View.OnKeyListener() { Integer count = 0; String typeWord = ""; public boolean onKey(View v, int keyCode, KeyEvent event) { if (KeyEvent.ACTION_DOWN == event.getAction()) { if (keyCode != 67) { count++; char c = (char)event.getUnicodeChar(); typeWord = typeWord + c; } else { count--; } if (count > 2 && typeWord != "") { countries = getAutosuggestWord(typeWord); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.autosuggest, countries); ed1.setAdapter(adapter); } } return false; } }); 
+7
source share
4 answers

You need to qualify the use of this when you want to refer to the surrounding class of the inner class. In your code, if the enclosing class is a subclass of Activity (suppose it's called MyActivity), you should write:

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyActivity.this, R.layout.autosuggest, countries); 
+23
source

The constructor is not defined because this is a reference to View.OnKeyListener . Use YourOuterClass.this .

+4
source

The this points to the current instance of the View.OnKeyListener class. The ArrayAdapter constructor takes the current context as the first parameter, which is your external class name, i.e. View (say MyActivity).

You should use MyActivity.this instead of this .

0
source

I know this is ridiculously late, but I usually use getActivity() in this circumstance. So it will look like this:

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.autosuggest, countries); ed1.setAdapter(adapter); 
0
source

All Articles