项目未从下拉列表中选择

时间:2015-09-06 05:35:16

标签: java android

未从“下拉菜单”中选择该项目。即使它被选中,为什么它不会出现在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: com.android.internal.view.IInputMethodClient$Stub$Proxy@2d9ab1ee attribute=null, token = android.os.BinderProxy@12992f4a
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: com.android.internal.view.IInputMethodClient$Stub$Proxy@2144bd25 attribute=null, token = android.os.BinderProxy@12992f4a

1 个答案:

答案 0 :(得分: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);
}