简体   繁体   中英

Wait for Input in Alert Dialog Spinner

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.

I've decided to use my common sense and simply create a second function that runs only when the detail is known. Here is my code incase anyone ever wants to do something like this. It prevents the user from closing the Dialog, and runs the parsing function from the onClick listener or right away if the detail is known.

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")){
                     parseData(changes, changes.getString("possibly missing detail"));
                 } else {
                     // todo: Ask user for detail
                     AlertDialog.Builder dialog_builder = new AlertDialog.Builder(MainActivity.this);
                     View mView = getLayoutInflater().inflate(R.layout.dialog_spinner, null);
                     dialog_builder.setMessage(R.string.need_detail_message);
                     dialog_builder.setTitle(R.string.need_detail_title);
                     final Spinner spinner = (Spinner) mView.findViewById(R.id.spinner);
                     List<String> detail_list = /* details from external class */;
                     detail_list.add(0, getString(R.string.need_detail_select));
                     ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_item, detail_list);
                     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                     spinner.setAdapter(adapter);

                     dialog_builder.setPositiveButton(R.string.CONTINUE_GENERAL, new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialog, int which) {
                             // Do nothing yet
                         }
                     });

                     dialog_builder.setView(mView);

                     final AlertDialog dialog = dialog_builder.create();
                     dialog.setCancelable(false);
                     dialog.setCanceledOnTouchOutside(false);
                     dialog.show();
                     dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                         @Override
                         public void onClick(View v) {
                             String selection = spinner.getSelectedItem().toString();
                             if (!selection.equals(getString(R.string.need_detail_select))){
                                 // todo: send to function
                                 parseData(changes, selection);
                                 dialog.dismiss();
                             } else {
                                 Toast.makeText(Tracking.this, R.string.need_detail_toast, Toast.LENGTH_SHORT).show();
                             }
                         }
                     });
                 }
             } else {
                 e.printStackTrace();
             }
         } catch (JSONException e){
             e.printStackTrace();
         }
     }
 };

 private void parseData(JSONObject changes, String detail){
     /* Do werk */
 }

Wow

Double-U, Oh, Double-U.

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