Cannot resolve constructor in ArrayAdapter using API data
Cannot resolve constructor in ArrayAdapter using API data
For a school project we are making an API based application and now we need to get data from an URL and add into an Spinner(Dropdown). Everything works (I think, haven't even gotten to test it) while getting the data, but inserting the data using an ArrayAdapter is giving me headaches.
Below is my FetchData class:
package com.example.prege.randomklasgenerator;
import android.app.Activity;
import android.os.AsyncTask;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
private String data = "";
private ArrayList<String> klaslist = new ArrayList<>();
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("http://mgroesink-001-site12.itempurl.com/api/student?studentclass=" + MainActivity.etKlas.getSelectedItem().toString());
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String jsonstring = bufferedReader.readLine();
if(jsonstring != null) {
try {
JSONArray klassen = new JSONArray(jsonstring);
for(int i = 0; i < klassen.length(); i++) {
String klasnaam = klassen.getString(i);
klaslist.add(klasnaam);
}
jsonstring = "";
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void aVoid) {
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(, android.R.layout.simple_spinner_item, klaslist);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
MainActivity.etKlas.setAdapter(dataAdapter);
}
}
On the onPostExecute I try to make an ArrayAdapter but it keeps giving me errors on the parameter where I need to give it Context.
4 Answers
4
Add Your classname.this just add this parameter
protected void onPostExecute(Void aVoid) {
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(FetchData.this, android.R.layout.simple_spinner_item, klaslist);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
MainActivity.etKlas.setAdapter(dataAdapter);
}
You're just missing this
. You have the comma there but nothing before it.
this
protected void onPostExecute(Void aVoid) {
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(ClassName.this, android.R.layout.simple_spinner_item, klaslist);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
MainActivity.etKlas.setAdapter(dataAdapter);
}
That should work now hopefully.
FetchData
Oh Yeah I see that now, cheers. Still a bit early over here
– Christopher
Jun 29 at 8:27
Yeah I left it empty since I did not know what to put here, normally I would put getApplicationContext() but I dont have access to this function somehow
– Nick Regeling
Jun 29 at 8:30
package com.example.prege.randomklasgenerator;
import android.app.Activity;
import android.os.AsyncTask;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
private String data = "";
private ArrayList<String> klaslist = new ArrayList<>();
//Make a constructor here - let name of class is MyClass
Context context;
public Myclass(Context context)
{
this.context = context;
}
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("http://mgroesink-001-site12.itempurl.com/api/student?studentclass=" + MainActivity.etKlas.getSelectedItem().toString());
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String jsonstring = bufferedReader.readLine();
if(jsonstring != null) {
try {
JSONArray klassen = new JSONArray(jsonstring);
for(int i = 0; i < klassen.length(); i++) {
String klasnaam = klassen.getString(i);
klaslist.add(klasnaam);
}
jsonstring = "";
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void aVoid) {
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, klaslist);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
MainActivity.etKlas.setAdapter(dataAdapter);
}
}
And when executing this class do like this:-
Myclass my = new Myclass(this);
my.execute();
This should definitely work for you.
**Try to change this method**
protected void onPostExecute(Void aVoid) {
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this.context, android.R.layout.simple_spinner_item, klaslist);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
MainActivity.etKlas.setAdapter(dataAdapter);
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Not necessarily. In most cases you would be right, but here the class is called
FetchData
which might not be an activity ;)– Gennadii Saprykin
Jun 29 at 8:26