spinner(SimpleCursorAdapter)数据库查询、获取所选中的值

数据库查询

    /*
     * 数据库查询
     */
    public Cursor queryData(String queryString) {
        database = SQLiteDatabase.openOrCreateDatabase(query_path, null);
        Cursor cursor = database.rawQuery(queryString, null);
        return cursor;// 返回游标
    }

查询结果填充到spinner

 Cursor cursor=queryData("select _id,name from cropUseRate");
                if(cursor!=null){
                    SimpleCursorAdapter adapter=new SimpleCursorAdapter(MainActivity.this, android.R.layout.simple_spinner_item, cursor,
                            new String[]{"name"}, new int[]{android.R.id.text1}, 0);
                    adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
                    spinner1.setAdapter(adapter);
                    }
                }

点击spinner获取选中的字符串

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub

                Cursor cursor=(Cursor)spinner1.getSelectedItem();
                s=cursor.getString(1);
                System.out.println(s);              
                }
            @Override
            public void onNothingSelected(AdapterView arg0) {
                // TODO Auto-generated method stub

            }
        });

你可能感兴趣的:(android开发)