I have a problem with making an new ArrayAdapter for a spinner in a android java thread. I cannot make this work because the thread has no link to my main class. I found on the Internet that instead of using a thread, I need to use an AsyncTask but for one reason I cannot get the principal of this. So I would like to know how I can solve this.
public void connect() throws InterruptedException
{
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet("http://192.168.1.118:8080/quiz/output.php");
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
// Examine the response status
//Log.i("Info",response.getStatusLine().toString()); Comes back with HTTP/1.1 200 OK
// Get hold of the response entity
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
jsonArray = new JSONArray(result);
instream.close();
}
} catch (Exception e) {
Log.e("Error",e.toString());
}
}
});
Thread thread2 = new Thread(new Runnable(){
@Override
public void run() {
List<String> list = new ArrayList<String>();
for (int i=0; i<jsonArray.length(); i++) {
try {
JSONObject item = jsonArray.getJSONObject(i);
String ID = item.getString("groepID");
String groepNaam = item.getString("groepNaam");
Log.e("string", "groepid = " + ID + " " + groepNaam);
} catch (JSONException e) {
e.printStackTrace();
}
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectgroep.setAdapter(dataAdapter);
}
});
thread.start();
thread.join();
thread2.start();
}
The error is with the following code:
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);
Would anyone know how I can solve this issue? Much appreciation, Thomas Thooft