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 // нужного объекта, передав ему результат } } } 

}

    1 answer 1

    1. Create an interface

       public interface MyInterface { void doSmth(JSONArray result); } 
    2. Implement it in activit

       public class UA extends AppCompatActivity implements MyInterface{ @Override public void doSmth(JSONArray result) { System.out.println("вызван doSmth в Активити"); //вот и ваш результат из задачи в актвити } ... } 
    3. Pass it to the task through the constructor and call its method, passing the result from onPostExecute :

       public final class AsyncRequest extends AsyncTask<String, Void, JSONArray> { MyInterface myInterface; public AsyncRequest(MyInterface myInterface){ this.myInterface = myInterface; } @Override protected void onPostExecute(JSONArray result) { System.out.println("вызван onPostExecute"); myInterface.doSmth(result); } } 
    4. In activation, run like this:

       new AsyncRequest(UA.this).execute(query); 
    • one
      Thank you very much! It helped a lot) - Vsevolod
    • Although here another question arises .. For some reason, onPostExecute starts to run only after I close the UA and, accordingly, before that nothing is updated (revealed a debug, for errors / exceptions it doesn’t pour out, but nothing appears in the seekbar - Vsevolod
    • @Vsevolod, I don’t see in your code that you were trying to do something with a sikBar. Plus make sure that in your task you are not trying to start another asynchronous task. In any case, try to remove everything from the task at all and simply return the devolt data from it. Based on this, try not to get the default and return. - JuriySPb
    • I apologize, stupid mistake, I meant spinner - Vsevolod
    • @Vsevolod, check to create a spinner with data without running the task. If it does not work, then the problem is not in the task. - Yuriy SPb