I saw this question about putting a spinner in an AlertDialog, but from what I can see the dialog displays, and the script continues on. It also quickly closes as soon as a choice is made. This would be good if you had an onDismiss
or onClick
listener that fires a separate function/ activity depending on what you selected, however this is not what I need.
Currently my app is polling a URL inside of a TimerTask
, and retrieving data. Inside this TimerTask
, the data is parsed and eventually saved. Depending on the data, it may be necessary to ask the user for additional details.
My goal is to wait until the user makes a selection from the spinner dialog, clicks the "OK" button, and then resume parsing the data and send it to another class for storage inside a database. How can I do this?
Timer timer = new Timer();
TimerTask checkApi = new TimerTask(){
@Override
public void run(){
try {
JSONObject changes = new JSONObject(/* External class that gets data from URL */);
if (changes.getBoolean("success")){
/* This is where the data is parsed */
/* Example skeleton code: */
JSONObject parsed_data = changes.getJSONObject("some_data");
if (changes.has("possibly missing detail")){
// This is where I want to wait for the selection to be made
String detail = dialogSpinnerResponse();
} else {
String detail = changes.getString("possibly missing detail");
}
parsed_data.put("important_detail", detail);
JSONObject reponse = new JSONObject(/* Result from sending parsed_data to external class for saving */);
// error handling if needed ...
} else {
e.printStackTrace();
}
} catch (JSONException e){
e.printStackTrace();
}
}
};
This timer task is initialized outside of any function, as a variable at the top of my Main class, and is started in the onCreate()
method of my activity.
I have tested this and it polls the url correctly and displays the unfiltered data. Now I am implementing the parsing part of it, where I need to wait for user input before sending it to my external class where it is saved.