0

我在为 android java 线程中的微调器制作新的 ArrayAdapter 时遇到问题。我无法完成这项工作,因为该线程与我的主类没有链接。我在 Internet 上发现,我需要使用 AsyncTask 而不是使用线程,但由于一个原因,我无法获得它的主体。所以我想知道如何解决这个问题。

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();

}

错误与以下代码有关:

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);

有人知道我该如何解决这个问题吗?非常感谢,Thomas Thooft

4

1 回答 1

2

在前面使用活动的类this

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(MyActivity.this,
    android.R.layout.simple_spinner_item, list);

在此处阅读有关此内容的更多信息:Java:Class.this

于 2013-09-09T18:28:09.293 回答