I'm using AlertDialog
with 1 EditText
. I want to know how i can send String from AlertDialog
to Fragment
.
Please check my code below.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_datasource){
enterdatasourceId();
return true;
}
return super.onOptionsItemSelected(item);
}
The code for enterdatasourceId is:
public void enterdatasourceId() {
LayoutInflater inflater = LayoutInflater.from(this);
final View dialogView = inflater.inflate(R.layout.cutom_dialog,null);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("Enter you unique ID");
dialogBuilder.setCancelable(false);
dialogBuilder.setView(dialogView);
final EditText datasource1 = (EditText) dialogView.findViewById(R.id.datasource_id);
dialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String dsource = datasource1.getText().toString().trim();
Intent intent = new Intent();
intent.putExtra("Datasource",dsource);
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog customDialog = dialogBuilder.create();
customDialog.show();
}
The following is the code of the fragment where i want to receive that String
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Datasource = getActivity().getIntent().getStringExtra("Datasource");
}
In Fragment
i want to receive the String that i entered in AlertDialog
and from here i'm sending that String as a parameter in my other class. I want to make it clear that the Activity
where i'm using AlertDialog
is dashboad2.java and the fragment where i want to receive that String is within the same Activity
(dashboard2.java)