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

前端 未结 4 1312
南笙
南笙 2021-01-18 01:54

Why The constructor ArrayAdapter(new View.OnKeyListener(){}, int, String[]) is undefined in following my coding. This coding is for fetching data from SQLite when typing wor

相关标签:
4条回答
  • 2021-01-18 02:29

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

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.autosuggest, countries);
    ed1.setAdapter(adapter); 
    
    0 讨论(0)
  • 2021-01-18 02:40

    You need to qualify the use of this when you want to refer to the enclosing class of an inner class. In your code, if the enclosing class is your Activity subclass (let's say it's called MyActivity), then you would write:

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyActivity.this,
       R.layout.autosuggest,
       countries);
    
    0 讨论(0)
  • 2021-01-18 02:41

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

    0 讨论(0)
  • 2021-01-18 02:42

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

    You should use MyActivity.this instead of this.

    0 讨论(0)
提交回复
热议问题