简体   繁体   中英

how can i receive value from AlertDialog(class) box in MainActivity(class) without passing control to AlertDialog class(Android Development)

Description: I have created AlertDialogBox Class, when i need the dialog box to pop-up i use alertDialog.show(), dialog box pop-up takes input as YES/NO(button as positive and negative) and stay on the same activity, but how can i get the value ie yes or no , and pass value enter by the user in the mainActivity from the dialog box. Tried:- I tried using bundle, using put/get and key value, but its return null value. tried using global variable but still null value.

Thanks for the help

You could do this by using an interface and the getActivity() and getParentFragment() methods of the DialogFragment class.

Note: I'm assuming (since you didn't post any code) that your already using a DialogFragment to show the AlertDialog.

First create the dialog and a special interface which can be used to pass the value to the owning Activity or Fragment :

public class MyAlertDialog extends DialogFragment implements DialogInterface.OnClickListener {

    public interface MyAlertDialogResultInterface {
        abstract void onButtonClicked(int button);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("Test message");
        builder.setPositiveButton("Ok", this);
        builder.setNegativeButton("No", this);
        return builder.create();
    }

    @Override
    public void onClick(DialogInterface dialogInterface, int button) {
        //Check if this DialogFragment is owned by a parent fragment
        if(getParentFragment() instanceof MyAlertDialogResultInterface){
            ((MyAlertDialogResultInterface) getParentFragment()).onButtonClicked(button);

        //Else check if this DialogFragment is owned by a parent activity
        } else if(getActivity() instanceof MyAlertDialogResultInterface){
            ((MyAlertDialogResultInterface) getActivity()).onButtonClicked(button);
        }
    }
}

Than add the special interface to your Activity and use the FragmentManager to show the dialog:

public class Test extends Activity implements MyAlertDialog.MyAlertDialogResultInterface {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Set up layout
    }

    private void showAlertDialog(){
        new MyAlertDialog().show(getFragmentManager(), "dialog-tag");
    }

    @Override
    public void onButtonClicked(int button) {
        //Do what ever you want to do
    }
}

Main Activity:

private void registerClickCallBack(){

    ListView list = (ListView)findViewById(R.id.starListView);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        StarClassValue clickedPosition = myStar.get(position);

        Str = clickedPosition.getStarName();
        String intr = clickedPosition.getIconNum() +"";
            //Toast.makeText(MainActivity.this,Str + intr,Toast.LENGTH_LONG).show();
            sendMessage(Str,intr);


        }
    });

    list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            StarClassValue click = myStar.get(position);
            String str = click.getStarName();


         dialog.show(getFragmentManager(),"Favourite");

            return false;
        }
    });
}

AlertDialog Class

public class AlertDialogFragment extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Context context = getActivity();
    final AlertDialog.Builder builder = new AlertDialog.Builder(context);

    builder.setTitle("Add to Favorite").setMessage("Do you want to add this artist to your favourite list").setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    }).setNegativeButton("Cancel", null);
    AlertDialog dialog = builder.create();


    return dialog;
}

}

So, how should i pass "yes" value received from AlertDialogFragment class into main activity without passing intent or control.

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