0

I am making an android application in which I want to populate a spinner with an array of string values, coming from SQLServer 2008. I am having a problem in which it says that the spinner cannot be populated from a thread different from the main thread.

       Thread waitth = new Thread() {
         public void run() {
             try {
                    System.out.println("3");

                 String details="mani";
                        System.out.println("4");
                        URLConnection con = getServletConnection();
                        System.out.println("5");
                        ObjectOutputStream oos = new ObjectOutputStream(con.getOutputStream());
                        System.out.println("7");
                        oos.writeObject(details);
                        System.out.println("8");
                        oos.flush();
                        oos.close();
                        // receive result from servlet
                        InputStream inputStream = con.getInputStream();
                        ObjectInputStream inputFromServlet = new ObjectInputStream(
                                inputStream);
                        result = (String[]) inputFromServlet.readObject();
                        //System.out.println(result);
                        inputFromServlet.close();
                        inputStream.close();
                        System.out.println();
                        System.out.println("got it");

                    System.out.println("450200");


                    ArrayList<String> aus=new ArrayList<String>();
                    for(int i=0;i<result.length;i++)
                    aus.add(result[i]);
                 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, aus);
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                   s1=(Spinner)findViewById(R.id.spinner1);

                        s1.setAdapter(adapter);




             }  
            catch (Exception ex) {
              ex.printStackTrace();
            }
         }
            };
            waitth.start();


    }


    private URLConnection getServletConnection() throws MalformedURLException,
       IOException {
    URL urlServlet = new URL("http://ipaddress/spinnerfordata");//servlet
    URLConnection con = urlServlet.openConnection();
    con.setUseCaches (false); 
       con.setDefaultUseCaches(false);
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setUseCaches(false);
    con.setRequestProperty("Content-Type","application/x-java-serialized-object");
    System.out.println("9");
    return con;

  }

Logcat error:

      08-02 18:49:08.439: W/System.err(918):                      android.view.ViewRootImpl$CalledFromWrongThreadException  : Only the original thread that created a view hierarchy can touch its views.
08-02 18:49:08.454: W/System.err(918):  at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
08-02 18:49:08.454: W/System.err(918):  at android.view.ViewRootImpl.focusableViewAvailable(ViewRootImpl.java:2588)
08-02 18:49:08.454: W/System.err(918):  at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
08-02 18:49:08.454: W/System.err(918):  at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
08-02 18:49:08.454: W/System.err(918):  at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
08-02 18:49:08.454: W/System.err(918):  at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
08-02 18:49:08.454: W/System.err(918):  at android.view.View.setFlags(View.java:8412)
08-02 18:49:08.464: W/System.err(918):  at android.view.View.setFocusable(View.java:5769)
08-02 18:49:08.464: W/System.err(918):  at android.widget.AdapterView.checkFocus(AdapterView.java:718)
08-02 18:49:08.464: W/System.err(918):  at android.widget.AbsSpinner.setAdapter(AbsSpinner.java:115)
08-02 18:49:08.464: W/System.err(918):  at android.widget.Spinner.setAdapter(Spinner.java:380)
08-02 18:49:08.464: W/System.err(918):  at com.example.manispinner.MainActivity$1.run(MainActivity.java:73)
Androiderson
  • 16,865
  • 6
  • 62
  • 72

2 Answers2

0

you are trying to update the UI from a non-UI thread, you cannot do that as the error says. anything relating to the UI needs to be done on the UI thread

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • look up how to use an AsyncTask and then after the task is done in your `onPostExecute` method which runs on the UI thread is where you update – tyczj Aug 02 '13 at 19:12
  • could u plz give me an example for that,,thnx in advance – user2643603 Aug 02 '13 at 19:31
  • return the array list you created in your `doInBackground` and you will have your list in the `onPostExecute` or create a global variable in your class and just access it that way – tyczj Aug 02 '13 at 19:51
  • it seems it doesn't recognise the array result in postexecute() method and this time i am returning the array from doInbackground() method but still result array is not recognised – user2643603 Aug 02 '13 at 20:16
  • oh god!! i think i just find out what i have to do to make the result array scope valid outside the thread,it was very silly and i doesn't have to make an async task as mentioned by u sir,but the help given by u is very very appreciated!!,,thnx.. – user2643603 Aug 03 '13 at 13:53
  • sir now i want to ask one more question,how can i make two threads synchronizable such that if one thread can't work until second runs and vice -versa – user2643603 Aug 04 '13 at 22:15
0
  1. Get the data using an API, from the server, either xml or json
  2. Parse the data.
  3. store it in an array.

    Do all the above in AsyncTask.
    
  4. Finally populate it to the spinner.

Find an example project at GitHub Spinner-Dynamic

VenomVendor
  • 15,064
  • 13
  • 65
  • 96