简体   繁体   中英

How to populate spinner data from a MS SQL Server database?

I am learning android and I'm stuck with the spinner, I need to get data from ms sql server. I've tried but on clicking the spinner nothing is happening, I've given Internet permission in manifest and also added jtds file.

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;
    }

I hope this helps you:

 /*
*  ******************************
*   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);

This works perfectly.

 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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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