1

I have created an app which includes popups of different dialogs.

Here is my code:

if (lDiffFromToday >= 0 && lDiffFromToday <= DeclareVariable.CYCLE_MAX_LENGTH)
{
    AlertDialog.Builder alrtStartMonitoring = new AlertDialog.Builder(this);
    alrtStartMonitoring.setTitle("              Start Monitoring");
    alrtStartMonitoring.setMessage("Set start date of cycle as"+" "+sdFormatter.format(dtSelDate));
    alrtStartMonitoring.setPositiveButton("Yes", this);
    AlertDialog alert = alrtStartMonitoring.create();
    alert.show();
}   
else if (dtSelDate.getTime()> dtStartDate.getTime() && dtSelDate.getTime() <= currentDate.getTime() && !bCycleStopped)
{
    long lDiffFromStart =dtSelDate.getTime()-dtStartDate.getTime();
    lDiffFromStart=lDiffFromStart/(1000 * 60 * 60 * 24);
    if (lDiffFromStart >= DeclareVariable.CYCLE_MIN_LENGTH)
    {
        bActionOk = true;
        AlertDialog.Builder alrtStartMonitoring = new AlertDialog.Builder(this);
        alrtStartMonitoring.setTitle("              Confirm New Cycle");
        alrtStartMonitoring.setMessage("Set start date of cycle as" + " " + sdFormatter.format(dtSelDate));
        alrtStartMonitoring.setPositiveButton("Yes", this);
        AlertDialog alert = alrtStartMonitoring.create();
        alert.show();
    }
}

// ...

public void onClick(DialogInterface dialog, int id) 
{
    CycleManager.getSingletonObject().setHistoryDate(dtSelDate);
    int iStopStartCount = CycleManager.getSingletonObject().getStopStartCount();                
    if(iStopStartCount>0)
        CycleManager.getSingletonObject().setStopStartDate(dtSelDate, iStopStartCount);
    displayDay();
}

Now my question is that for each dialog I need different onClick functions but in my case when I write another onClick function then there will be conflict. I know by writing the onClick function inside each dialog may solve the problem but in that case, I have to declare my variables as final so how can I do it by writing onClick function outside for every dialog I used.

m02ph3u5
  • 3,022
  • 7
  • 38
  • 51
AndroidDev
  • 4,521
  • 24
  • 78
  • 126

3 Answers3

1

Another solution will be to make the AlertDialog instances members of your class. Then in the OnClick method:

public void onClick(DialogInterface dialog, int id) 
{
  if (dialog == m_Dialog1)
  {
    // server dialog 1
  }

}
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • is this correct? If you're implementing the onClick from [DialogInterface.OnClick()](http://developer.android.com/reference/android/content/DialogInterface.OnClickListener.html) then the id is the button id that was pressed. – Mark Fisher Jul 01 '11 at 12:21
  • Yes, but I'm not checking the Id, but the dialog itself. – kgiannakakis Jul 01 '11 at 12:29
0

I can see that you've made the your class to implement the DialogInterface.OnClickListener

instead of

alrtStartMonitoring.setPositiveButton("Yes", this);

You can make it this way

alrtStartMonitoring.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

public void onClick (DialogInterface dialog, int which) {

}
});

and for each one of the setPositiveButton you can define the different onClick listener

Hope this helps

mohamede1945
  • 7,092
  • 6
  • 47
  • 61
0

Why not create your own DialogInterface.OnClickListener classes and instantiate them with the appropriate variables from your main class that you don't want to mark as final, but do want them to have access to (inject them effectively). Then you can do

FooDialogOnClickListener l1 = new FooDialogOnClickListener(dtSelData, ...);
BarDialogOnClickListener l2 = new BarDialogOnClickListener(iStopStartCount, ...);

if (...) {
    // ...
    alrtStartMonitoring.setPositiveButton("Yes", l1);
} else {
    // ...
    alrtStartMonitoring.setPositiveButton("Yes", l2);
}
Mark Fisher
  • 9,838
  • 3
  • 32
  • 38