Context:
Cases which involve using a custom dialog to capture user input, may require the captured data to be passed back to the displaying activity. The following describes exactly this… how to pass back data from a custom dialog to the displaying activity.
ps – by custom dialog we mean a custom class that ‘extends Dialog‘.
Solution:
Step 1 – Define custom interface
// step 1 - to return values from dialog public interface OnFooEventListener { public void fooEvent(int fieldOne, int fieldTwo); }Step 2 – Declare the above interface as a field member
// step 2 - to return values from dialog private OnFooEventListener onFooEventListener;Step 3 – Add it to the custom dialog constructor
// step 3 - to return values from dialog public FooEventDialog(Context context, OnFooEventListener onFooEventListener) { super(context); this.context = context; this.onFooEventListener = onFooEventListener; }Step 4 – Call it within the view object’s event listener, (in this case a radio button)
Button btnOk = (Button) findViewById(R.id.btnSet); btnOk.setOnClickListener( new Button.OnClickListener() { @Override public void onClick(View v) { // step 4 - to return values from dialog onFooEventListener.fooEvent(fieldOne, fieldTwo); dismiss(); } });Overview – So the class should look something like the following
public class FooEventDialog extends Dialog { // step 1 public interface OnFooEventListener { public void fooEvent(int fieldOne, int fieldTwo); }// step 2
private OnFooEventListener onFooEventListener;
// Other member fields
...
// step 3
public FooEventDialog(Context context, OnFooEventListener onFooEventListener)
{ ... }
@Override
public void onCreate(Bundle savedInstanceState)
{
// other stuff
...
Button btnOk = (Button) findViewById(R.id.btnSet);
btnOk.setOnClickListener( new Button.OnClickListener()
{
@Override public void onClick(View v) { // step 4 - to return values from dialog onFooEventListener.fooEvent(fieldOne, fieldTwo); dismiss(); } }); } }… That’s all there is to it.
Wa’Allahu A’lam !
[Via http://ibnaziz.wordpress.com]
No comments:
Post a Comment