Reputation: 103
I need pass data from from MainActivity to Dialog in Android Studio so that when AlertDialog is launched, the value I have sent from the MainActivity can be shown in it. Until now with this code I have only managed to pass data from the Dialog to the MainActivity but not in the opposite way The data i want to send is:
IPAdreess = "192.1681.1.4";
IPPort ="8089";
Here is my MainActivity:
public class MainActivity extends AppCompatActivity implements ConfigWIFIDialog.ConfigWIFIDialogListener {
private String IPAdreess = "192.1681.1.4";
private String IPPort ="8089";
private TextView myIPAdreess;
private TextView myIPPort;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myIPAdreess = (TextView) findViewById(R.id.txv_ipAddress);
myIPPort = (TextView) findViewById(R.id.txv_ipPort);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialog();
}
});
}
public void openDialog() {
ConfigWIFIDialog configWIFIDialog = new ConfigWIFIDialog();
configWIFIDialog.show(getSupportFragmentManager(), "example dialog");
}
@Override
public void applyTexts(String mIpAddreess, String mIpPort) {
myIPAdreess.setText(mIpAddreess);
myIPPort.setText(mIpPort);
}
}
And here is the Dialog:
public class ConfigWIFIDialog extends AppCompatDialogFragment {
private EditText myIpAddress;
private EditText myIpPort;
private ConfigWIFIDialogListener listener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.config_wifi_dialog, null);
myIpAddress = (EditText) view.findViewById(R.id.etx_ipAddress);
myIpPort = view.findViewById(R.id.etx_ipPort);
builder.setView(view)
.setTitle("Configuracion WIFI")
.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String mIpAddreess = myIpAddress.getText().toString();
String mIpPort = myIpPort.getText().toString();
listener.applyTexts(mIpAddreess, mIpPort);
}
});
return builder.create();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
listener = (ConfigWIFIDialogListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() +
"must implement ConfigWIFIDialogListener");
}
}
public interface ConfigWIFIDialogListener {
void applyTexts(String mIpAddreess, String mIpPort);
}
}
Upvotes: 0
Views: 982
Reputation: 1086
You are implementing AppCompatDialogFragment, you can pass data to it by setting its arguments
public void openDialog() {
ConfigWIFIDialog configWIFIDialog = new ConfigWIFIDialog();
Bundle args = new Bundle();
args.putString("title", title);
configWIFIDialog.setArguments(args);
configWIFIDialog.show(getSupportFragmentManager(), "example dialog");
}
you can learn more from codepath
Upvotes: 1