Trying to create an adapter to fill a single listView.

runOnUiThread(new Runnable() { @Override public void run() { ListView listView = (ListView)findViewById(R.id.listView); ArrayAdapter<String> adapter; adapter = new ArrayAdapter<String>(this, R.layout.dialogs, R.id.listView, Dialogs); listView.setAdapter(adapter); } }); 

Do not compile, swears at these arguments:

 (this, R.layout.dialogs, R.id.listView, Dialogs) 

PS Dialogs - String global array

What am I doing wrong? Error: cannot resolve constructor

    2 answers 2

    If you get the message Cannot resolve constructor - it is impossible to determine the constructor, it 's time to see which constructors the ArrayAdapter has in ArrayAdapter :

    ArrayAdapter(Context context, int resource)

    • Where:
      context - context
      resource - resource ID with markup for one item in the list, type R.layout.item .

    ArrayAdapter(Context context, int resource, int textViewResourceId)

    • Where:
      context - context
      resource - a link to a resource with markup for an item of the form R.layout.item ,
      textViewResourceId - the resource ID pointing to the TextView in the item markup to which data will be displayed, of the type R.id.textView .

    ArrayAdapter(Context context, int resource, T[] objects)

    • Where:
      context - context
      resource - resource ID with markup for one item in the list, of the type R.layout.item ,
      objects - an array of data in the form of objects to display in the list.

    ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)

    • Where:
      context - context
      resource - a link to a resource with markup for an item of the form R.layout.item ,
      textViewResourceId - the resource ID pointing to the TextView in the item markup to which data will be displayed, of the type R.id.textView ,
      objects - an array of data in the form of objects to display in the list.

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

    • Where:
      context - context
      resource - resource ID with markup for one item in the list, of the type R.layout.item ,
      objects - a collection of data to display.

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

    • Where:
      context - context
      resource - resource ID with markup for one item in the list, of the type R.layout.item ,
      textViewResourceId - the resource ID pointing to the TextView in the item markup to which data will be displayed, of the type R.id.textView ,
      objects - a collection of data to display.

    Having carefully examined all this variety of designers, it is easy to see that you managed to create a designer that the programmers of the unfortunate adapter did not provide.

    In particular, by your example:

     adapter = new ArrayAdapter<String>(this, R.layout.dialogs, R.id.listView, Dialogs); 

    this - the service word refers to an instance of the current object, while the constructor requires a context. This as a context can only be used for activation, as it is a heir to the context - for all other classes, the context must be explicit, including for your Runnable class.

    R.layout.dialogs - let's hope that this is the markup of exactly one item (one element of the list), although doubts appear on the name chosen for it

    R.id.listView - the third parameter should be the ID TextView in the item (which should be in the R.layout.dialogs markup in your case), where the data should be output, but not the ID of the entire ListView .
    If there is one TextView in the list for displaying data, then this parameter is better not to be specified in the constructor.

    Dialogs is also an unknown entity, in the place of which there must be an array, or a collection of data that should be displayed in the list. By name it looks like it is some kind of static class, since it starts with a capital letter and if this is so, there is no place for this class in the constructor.

    • The solution was the following code: ListView listView = (ListView) findViewById (R.id.listView); ArrayAdapter <String> adapter = new ArrayAdapter <String> (dialogs.this, android.R.layout.simple_list_item_1, Dialogs); listView.setAdapter (adapter); - Nikola Krivosheya
    • one
      The funny thing is that the code I gave at the beginning was absolutely correct)) so I thought, if a person asks a similar question, the problem is simpler than it seems. In any case, your answer is +, for a very detailed analysis. - Shwarz Andrei

    I think the arguments of an error in this .

    You need to pass the Context, and you pass the Runnable object.

    If the code is called in an activation (for example, in MainActivity), try passing MainActivity.this instead of this.