2015-09-06 2 views
0

Элемент не выбирается из раскрывающегося меню. Даже если он выбирается, почему он не появляется в TextView?элемент не выбирается из выпадающего списка

Кроме того, я получаю эти странные точки после каждого элемента, который я извлекаю из базы данных Microsoft SQL Server.

Image here

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    String choosenItem = "nothing"; 
    Connection connect; 
    PreparedStatement preparedStatement; 
    Statement st; 
    String[] list; 
    List<String> ArrayListing =new ArrayList<String>(); 

    @SuppressLint("NewApi") 
    private Connection ConnectionHelper() { 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
       .permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 
     Connection connection = null; 
     String ConnectionURL = null; 
     try { 
      Class.forName("net.sourceforge.jtds.jdbc.Driver"); 
      Log.e("ERRO", "Class Loaded"); 
      ConnectionURL = "jdbc:jtds:sqlserver://localhost/portal;encrypt=fasle;user=cp_t;password=cp3;instance=MSSQLSERVER;"; 
      connection = DriverManager.getConnection(ConnectionURL); 
      Log.e("ERRO", "Connection Established"); 
     } catch (SQLException se) { 
      Log.e("ERRO", se.getMessage()); 
     } catch (ClassNotFoundException e) { 
      Log.e("ERRO", e.getMessage()); 
     } catch (Exception e) { 
      Log.e("ERRO", e.getMessage()); 
     } 
     return connection; 
    } 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       try { 
        connect = ConnectionHelper(); 

        st = connect.createStatement(); 
        ResultSet rs = st.executeQuery("select Items from drop_down"); 
        Log.d("ERRO", "Result set executed"); 
        Array z = null; 
        String s; 
        while(rs.next()){ 

         s = rs.getString(1); 
         Log.d("Database Strings", s); 

         ArrayListing.add(s); 
         Log.d("Feeding array", "did it"); 
         //z = rs.getArray("Items"); 
        } 

        //list = (String[])z.getArray(); 

       } catch (SQLException e) { 
        Log.e("ERRO", "Runnable Error"); 
        e.printStackTrace(); 
       } 
      } 
     }; 

     Thread t = new Thread(r); 
     t.start(); 

     Spinner dynamicSpinner = (Spinner) findViewById(R.id.dynamic_spinner); 

     String[] items = ArrayListing.toArray(new String[ArrayListing.size()]); 

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

     dynamicSpinner.setAdapter(adapter); 

     dynamicSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, 
             int position, long id) { 

       choosenItem = (String) parent.getItemAtPosition(position); 
       Log.v("item", choosenItem); 
       TextView show = (TextView) findViewById(R.id.show); 

       show.setText(choosenItem); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 
       // TODO Auto-generated method stub 
      } 
     }); 


    } 
} 

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 

    <Spinner 
     android:id="@+id/dynamic_spinner" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="HERE" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="34dp" 
     android:id="@+id/show" /> 

</RelativeLayout> 

Журналы:

09-06 11:03:38.929 2369-2385/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented 
09-06 11:03:38.929 2369-2385/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa67d7340, error=EGL_SUCCESS 
09-06 11:03:39.028 1223-1253/? I/ActivityManager﹕ Displayed com.muchmore.www.dropdown/.MainActivity: +430ms 
09-06 11:03:45.202 2369-2384/? E/ERRO﹕ Connection Established 
09-06 11:03:45.277 2369-2384/? D/ERRO﹕ Result set executed 
09-06 11:03:45.277 2369-2384/? D/Database Strings﹕ Ground 
09-06 11:03:45.277 2369-2384/? D/Feeding array﹕ did it 
09-06 11:03:45.277 2369-2384/? D/Database Strings﹕ Classes 
09-06 11:03:45.277 2369-2384/? D/Feeding array﹕ did it 
09-06 11:03:45.278 2369-2384/? D/Database Strings﹕ Labs 
09-06 11:03:45.278 2369-2384/? D/Feeding array﹕ did it 
09-06 11:03:45.278 2369-2384/? D/Database Strings﹕ Buildings 
09-06 11:03:45.278 2369-2384/? D/Feeding array﹕ did it 
09-06 11:03:45.278 2369-2384/? D/Database Strings﹕ Libraries 
09-06 11:03:45.278 2369-2384/? D/Feeding array﹕ did it 
09-06 11:03:52.439 2369-2369/? W/art﹕ Before Android 4.1, method int android.support.v7.internal.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 
09-06 11:03:52.486 2369-2385/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented 
09-06 11:03:52.486 2369-2385/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa644a0a0, error=EGL_SUCCESS 
09-06 11:03:54.452 1223-1277/? W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client 
09-06 11:03:54.473 2369-2369/? W/InputEventReceiver﹕ Attempted to finish an input event but the input event receiver has already been disposed. 
09-06 11:03:54.478 1223-1237/? W/InputMethodManagerService﹕ Window already focused, ignoring focus gain of: [email protected] attribute=null, token = [email protected] 
09-06 11:03:56.631 2369-2385/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented 
09-06 11:03:56.631 2369-2385/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa64e9500, error=EGL_SUCCESS 
09-06 11:03:57.388 1223-1277/? W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client 
09-06 11:03:57.396 2369-2369/? W/InputEventReceiver﹕ Attempted to finish an input event but the input event receiver has already been disposed. 
09-06 11:03:57.399 1223-1514/? W/InputMethodManagerService﹕ Window already focused, ignoring focus gain of: [email protected] attribute=null, token = [email protected] 
+0

Вы не должны пройти пунктов [] массив вместо ArrayListing в адаптере? – Jerry

+0

Если я передаю элементы [], DropDown даже не получает элементы –

ответ

0

Попробуйте

@Override 
public void onItemSelected(AdapterView<?> parent, View view, 
            int position, long id) { 
    choosenItem = dynamicSpinner.getItemAtPosition(position).toString(); 
    Log.v("item", choosenItem); 
    TextView show = (TextView) findViewById(R.id.show); 
    show.setText(choosenItem); 
} 
+0

не работает ... Я думаю, проблема в том, что массив и коллекция где-то там –

Смежные вопросы