Question:
There is a window with a spinner
that should be filled with values from the MS SQL database. To communicate with the database, I use jtds 1.2.8 in asynchronous requests, but I can't find a way to forward the query result data to the activity that sent the asynchronous request. How do I implement the filling of the spinner
after the request has been executed?
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
// нужного объекта, передав ему результат
}
}
}
}
Answer:
-
Create an interface
public interface MyInterface { void doSmth(JSONArray result); }
-
Implement it in activity
public class UA extends AppCompatActivity implements MyInterface{ @Override public void doSmth(JSONArray result) { System.out.println("вызван doSmth в Активити"); //вот и ваш результат из задачи в актвити } ... }
-
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); } }
-
In activation, run like this:
new AsyncRequest(UA.this).execute(query);