I have an activity with two fragments: one for showing products in a grid view, and the other to show the products that the user adds to the order (ListFragment). When the u
The recommended approach is to communicate from the DialogFragment to the Activity using an Interface, and then from the Activity to the Fragment.
In your activity:
public class Main extends FragmentActivity implements OnQuantitySelectedListener {
public interface OnQuantitySelectedListener {
void onFinishEditDialog(String inputText);
}
@Override
public void onFinishEditDialog(String inputText) {
Toast.makeText(this, "Quantity: " + inputText, Toast.LENGTH_SHORT).show();
}
}
Then the DialogFragment inner class
public static class ProductDialog extends DialogFragment {
static ProductDialog newInstance(ProductVO product) {
ProductDialog f = new ProductDialog();
Bundle args = new Bundle();
args.putSerializable("product", product);
f.setArguments(args);
return f;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
ProductVO product = (ProductVO) getArguments().getSerializable("product");
LayoutInflater factory = LayoutInflater.from(getActivity());
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
mEditText = (EditText) textEntryView.findViewById(R.id.txt_your_name);
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.ic_dialog_add)
.setTitle(R.string.add_product)
.setView(textEntryView)
.setPositiveButton(R.string.accept,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
OnQuantitySelectedListener listener = (OnQuantitySelectedListener) getActivity();
listener.onFinishEditDialog(mEditText.getText().toString());
}
}
)
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}
)
.create();
}
}
The XML for R.layout.alert_dialog_text_entry is from the API Demos. It doesn't fit your use case of getting a quantity from the user, but it illustrates using a custom layout to get a value from the user.