如何从MS SQL Server数据库填充微调器数据?

时间:2018-08-24 12:04:02

标签: android sql sql-server

我正在学习android,但我一直不停地使用微调器,我需要从ms sql服务器获取数据。我已经尝试过了,但是在单击微调器上没有任何反应,我在清单中授予了Internet权限,还添加了jtds文件。

state.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
       @Override
       public void onItemSelected(AdapterView<?> adapterView, View view, int 
        i, long l) {
      // I'll call CenterState here
       }

       @Override
       public void onNothingSelected(AdapterView<?> adapterView) {

       }
   });
}

public class CenterState extends AsyncTask<String, String, String> {
    String message = "";
    boolean isSuccess = false;

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            Connection con = connectionClass.CONN();
            if (con == null) {
                message = "Error in connection!!";
            } else {
                String query = "select email from registration";
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(query);
                String id = rs.getString("email");
                stateSpinner.add(id);

                if (rs.next()) {
                    isSuccess = true;
                } else {
                }
                ArrayAdapter arrayAdapter = new 
ArrayAdapter(CenterVisitActivity.this,R.layout.spinner_item,
                        stateSpinner);
                state.setAdapter(arrayAdapter);
            }
        } catch (Exception ex) {
            isSuccess = false;
            return ex.getMessage();
        }
        return message;
    }

2 个答案:

答案 0 :(得分:0)

希望这对您有所帮助

 /*
*  ******************************
*   Gett all Values for Spinner
*  ******************************
*/

List<String> emailList = new ArrayList<>();

try {
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            try {
            Connection con = connectionClass.CONN();
            if (con == null) {
                message = "Error in connection!!";
            } else {
                String query = "select email from registration";
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(query);

                //If tehre are several results you will nee to loop through all results
                foreach(String email : rs){
                    emailList.add(email.getString("email"))
                }

            }
        } catch (Exception ex) {
            isSuccess = false;
            return ex.getMessage();
        }
        return message;
        }
    }.execute().get();
} catch (InterruptedException | ExecutionException e) {
   //handle error
}

String[] emailArray = new String[emailList.size()];

emailArray = emailList.toArray(emailArray);


/*
*  **************************
*   Spinner Email
*  **************************
*/
spinnerEmail = (Spinner) findViewById(R.id.spinner_Email);

// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<String> adapterEmail = new ArrayAdapter<>(Activity.this,
        R.layout.spinner_item_single_text_line, emailArray);

// Specify the layout to use when the list of choices appears
adapterEmail.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// Apply the adapter to the spinner
spinnerEmail.setAdapter(adapterEmail);

答案 1 :(得分:0)

这很好用。

 try {
        Connection con = connectionClass.CONN();
        if (con == null) {
            Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
        } else {
            String query = "select * from registration";
            stmt = con.prepareStatement(query);
            rs = stmt.executeQuery();
            ArrayList<String> data = new ArrayList<>();
            while (rs.next()) {
                String id = rs.getString("email");// value of database
                data.add(id);
            }
            ArrayAdapter NoCoreAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);
            state.setAdapter(NoCoreAdapter);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }