简体   繁体   English

如何使用来自SQL Server 2008的动态数据填充微调器

[英]How to populate the spinner with the dynamic data coming from sql server 2008

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. 我正在制作一个Android应用程序,其中要使用来自SQLServer 2008的字符串值数组填充微调器。我遇到一个问题,即它无法从不同于主线程的线程填充微调器。

       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: Logcat错误:

      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)

you are trying to update the UI from a non-UI thread, you cannot do that as the error says. 您尝试从非UI线程更新UI,则无法执行该操作(错误提示)。 anything relating to the UI needs to be done on the UI thread 与UI相关的任何事情都需要在UI线程上完成

  1. Get the data using an API, from the server, either xml or json 使用API​​从服务器获取数据( xmljson
  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 在GitHub Spinner-Dynamic上找到示例项目

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

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