Phenom
Phenom

Reputation: 23

Is there a way to tell what button is pressed to bring up an alert dialog?

I have an alert dialog that pops up when a button is pressed. The problem is I need to know what button was pressed to bring up this alert as it's text is the number I need to make a call to my database.

Basically I need to know if there is a way to pass the button as a parameter to bring up the alert dialog box (if possible) or any other easier way to get the text that is on the button that was pressed to bring said alert up.

Buttons are named in the fashion of p1, p2, p3 etc for player 1, player 2, player3 et

There are 21 of these buttons so being able to handle all of the buttons in one place just makes the code quite a bit more manageable

the increments in there are just variable to update textviews

My alert code:

final AlertDialog.Builder popit = new AlertDialog.Builder(this);
    popit.setMessage("Shot, Assist or Goal?");
    popit.setPositiveButton("Shot", new DialogInterface.OnClickListener() {         
        @Override
        public void onClick(DialogInterface dialog, int which) {
            homeshots++;
            hshot.setText("S: " + homeshots);
            //dbcallhere                
        }
    });
    popit.setNegativeButton("Goal", new DialogInterface.OnClickListener() {         
        @Override
        public void onClick(DialogInterface dialog, int which) {
            homegoals++;
            homeshots++;
            hgoal.setText("G: " + homegoals);
            hshot.setText("S: " + homeshots);
            //dbcall here           
        }
    });
    popit.setNeutralButton("Assist", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //dbcall 
        }
    });

Thanks to everyone who reads this in advance.

Upvotes: 0

Views: 160

Answers (3)

Murtaza Khursheed Hussain
Murtaza Khursheed Hussain

Reputation: 15336

there is very simple soulution to know that.

  1. If your buttons are in xml, set attribute android:settag=""
  2. If your button are programatically generated, then call yourButton.setTag((String) "someId");
  3. To Know which button is clicked

    yourButton.setClickListener(this);

then

@Override

public void onClick(View v) {
   switch(v.getid){
     case R.id.button1:
        return myTag(v);
     break;
   }
}

public String myTag(View v){
   return (String)((Button)v).getTag();
}

Upvotes: 0

rekire
rekire

Reputation: 47965

I guess you have multiple buttons which open your dialog. In that case you should be able to know which button was pressed at that point where you add your onclick listener to the button which opens dialog.

With this knowledge you can set a final field in the same scope of your alert builder code. That you can read the final field in your DialogInterface.OnClickListener and you are done.

Upvotes: 0

archon92
archon92

Reputation: 447

public void onClick(View v) {
switch(v.getId())
{
case R.id.button_a_id:
// handle button A click;
break;
case R.id.button_b_id:
// handle button B click;
break;
default:
throw new RuntimeException("Unknow button ID");
}

This makes sure you dont miss to handle any button clicks.

Upvotes: 1

Related Questions