简体   繁体   中英

Returning information from an AlertDialog in Android

I have created the following AlertDialog . In it, the user selects an item from a range of options stored in an XML file and handled using an Adapter , and then clicks either the positive button or the negative button. Here is the code:

public void OpenDialog() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
        dialog.setTitle("Promotion Options");
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(activity.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(com.zlaporta.chessgame.R.layout.promotion, null);

        dialog.setView(v);
        dialog.setPositiveButton("Choose", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == 1) {
                    System.out.println("ok");
                }
            }
        });
        dialog.setNegativeButton("Undo Move", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog.show();

        Spinner spinner = (Spinner) v.findViewById(com.zlaporta.chessgame.R.id.promotionSpin);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(activity, com.zlaporta.chessgame.R.array.option,
                android.R.layout.simple_spinner_item);
        //could be other options here instead of simple_spinner_dropdown_item. Just type simple and see what comes up next time
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

Now, I'd like information about the user's selection in the spinner to be passed back to the enclosing Activity after the positive button is clicked. What's the best way to do this?

As you seem to have defined the AlertDialog in a separate class, you cannot directly access methods defined inside the Activity from where it is invoked.

One possible way is to define a public method inside the Activity as follows:

public void doSomething(Object spinnerDataObject){

    ....

}

and access it this way:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        // call the Activity's method here and send the selected item.          
        ((MainActivity)activity).doSomething(parent.getItemAtPosition(position));
        dialog.dismiss();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
});

This will send the selected Spinner data object to the Activity . Whatever you want to do with that object can be done inside doSomething() .

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