0

Im trying to create an Array Adaptor from an ArrayList

 ArrayList<String> serverarray ;
 ArrayAdapter<String> arrayAdapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,serverarray);

But i keep getting this

The constructor ArrayAdapter<String>(MainActivity.FindServer, int, ArrayList<String>) is undefined

What im i doing wrong?

techno
  • 5,884
  • 15
  • 76
  • 169

2 Answers2

2

use

 ArrayAdapter<String> arrayAdapter =new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,serverarray);

this in your current context is MainActivity.FindServer which is not a valid Context object i believe.

Shubhank
  • 21,643
  • 8
  • 64
  • 83
1

The constructor you are trying to invoke is of signature:

ArrayAdapter(Context context, int resource, List<T> objects) 

The 2nd and third 3rd arguments seem okay, so probably this as a first argument is not of type Context.

You need to pass a reference to the context of MainActivity in your MainActivity.FindServer class, and then pass that as your first argument.

I'd suggest having a look here: How to retrieve a context from a non-activity class?

ᴘᴀɴᴀʏɪᴏᴛɪs
  • 6,559
  • 7
  • 48
  • 76