I am trying to do something unusual. I have a main activity with a menu attached to the menu button. One item in the menu opens a dialog to pick a control that is added to the main activity. I have it working so it adds the control, and saves it in a database (so it will be remembered for the next run). I need to set the button's onClickListener to the main activity's onClick.
public class MyMainActivity extends Activity
implements View.OnClickListener, View.OnTouchListener
{
private Context mContext;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
AbsoluteLayout mMainActivityView = new AbsoluteLayout;
SetContentView(mMainActivityView);
... populate mMainActivityView from database ...
public void onClick(View v) {
switch (v.id) {
case NEW_BUTTON_ID:
// TODO implement click handler
}
}
...
...
protected Dialog onCreateDialog(int id) {
switch (id) {
case NEW_BUTTON_DIALOG_ID:
...
...
builder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
int XPos = Integer.valueOf(editXPos.getText().toString());
int YPos = Integer.valueOf(editYPos.getText().toString());
mDataLayer.AddControl(mScreenID, Width, Height, XPos, YPos, editButtonText.getText().toString());
Button button = new Button (mContext);
button.setLayoutParams(new AbsoluteLayout.LayoutParams(Width, Height, XPos, YPos));
button.setText(editButtonText.getText().toString());
mMainActivityView.addView(button);
// How to set Listeners from main activity?
button.setOnClickListener(?????);
button.setOnTouchListener(?????);
MyMainActivity.this.removeDialog(NEW_BUTTON_DIALOG_ID);
}
});
builder.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
MyMainActivity.this.removeDialog(NEW_BUTTON_DIALOG_ID);
}
});
AlertDialog NewButtonDialog = builder.create();
return NewButtonDialog;
}
return null;
}
So, how do I reverence the main activity's onClick()
from withing the AlertDialog's button?