简体   繁体   English

android java线程中的新ArrayAdapter

[英]New ArrayAdapter in android java thread

I have a problem with making an new ArrayAdapter for a spinner in a android java thread. 我在android java线程中为spinner创建一个新的ArrayAdapter时遇到问题。 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. 我在互联网上发现,我不需要使用线程,而是需要使用AsyncTask,但由于一个原因,我无法得到这个原理。 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 非常感谢,Thomas Thooft

Use the activity's class in front of this : 使用活动的类面前this

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

Read more about this here: Java: Class.this 在这里阅读更多相关信息: Java:Class.this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM