There is a window with a spinner element, which must be filled with values from the MS SQL database. To communicate with the database I use jtds 1.2.8 in asynchronous requests, but it does not work out to find a way to transport the data of the query result to the activity that sent the asynchronous request. How to implement spinner filling after the request?
UA.java
public class UA extends AppCompatActivity{ private QueryItem Item = new QueryItem(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ua); Item.setId(Search_Page.id_intent); Search_Page.id_intent = 0; String[] query = new String[2]; query[1] = "get_dish_links"; query[0] = "EXEC " + query[1] + "" + Item.getId(); new AsyncRequest().execute(query); final SeekBar seekbar = (SeekBar) findViewById(R.id.seekBar); } public void onClick(View view) { final Spinner spinner = (Spinner)findViewById(R.id.spinner); //Item.setUnit(spinner); } public void fill_spinner(List<Link> links){ final Spinner spinner = (Spinner)findViewById(R.id.spinner); ArrayAdapter<Link> possible_units = new ArrayAdapter<Link>(this, android.R.layout.simple_list_item_1, links); spinner.setAdapter(possible_units); spinner.setSelection(0); possible_units.notifyDataSetChanged(); } AsyncRequest.java
public final class AsyncRequest extends AsyncTask<String, Void, JSONArray> { public final static String MSSQL_DB = "jdbc:jtds:sqlserver://localhost:1433; DatabaseName=Lazycook"; public final static String MSSQL_LOGIN = "lazyuser"; public final static String MSSQL_PASS = "12344321"; public final static JSONConverter conv = new JSONConverter(); public String action; @Override protected JSONArray doInBackground(String[] query) { JSONArray resultSet = new JSONArray(); action = query[1]; try { Class.forName("net.sourceforge.jtds.jdbc.Driver"); Connection con = null; Statement st = null; ResultSet rs = null; try { con = DriverManager.getConnection(MSSQL_DB, MSSQL_LOGIN, MSSQL_PASS); if (con != null) { st = con.createStatement(); rs = st.executeQuery(query[0]); if (rs != null) { int columnCount = rs.getMetaData().getColumnCount(); // Сохранение данных в JSONArray while (rs.next()) { JSONObject rowObject = new JSONObject(); for (int i = 1; i <= columnCount; i++) { rowObject.put(rs.getMetaData().getColumnName(i), (rs.getString(i) != null) ? rs.getString(i) : ""); } resultSet.put(rowObject); } } } } catch (SQLException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (st != null) st.close(); if (con != null) con.close(); } catch (SQLException e) { throw new RuntimeException(e.getMessage()); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } return resultSet; } @Override protected void onPostExecute(JSONArray result) { switch (action){ case "get_dish_links" : { List<Link> links = conv.JSONtoLink(result); // вот здесь думал использовать вызов fill_spinner // нужного объекта, передав ему результат } } } }