Problem getting the ID name

1

I have several buttons, I assign this function, what I want to get the name of the ID depending on the botón that has been pressed, I could already get the whole value (of the identifier) but I need the String of the identifier

public  void BorrarProducto (View v)
{
   int resID = v.getId();
   String NombreID = getString(resID);
   CantidadProductos.setText("" + NombreID);
}
    
asked by Jesus Alberto Romero 04.05.2017 в 23:23
source

3 answers

1

Since the event where you invoke calls the ev.getSource() that it invokes from which component the event is being executed, something like that should be there, print the button pressed. yes only if all your buttons are registered btn.addActionListener(this); to the same event:

public void actionPerformed(ActionEvent ev){
   Object fuente = ev.getSource;
   System.out.printLn(fuente.getClass().getName());
}
    
answered by 04.05.2017 в 23:37
1

You can do this using the getResourceEntryName ()

method a> , which you would use in this way:

int resID = v.getId();
//Obtiene el nombre de la vista mediante el id definido en R.java
String nombre = getResourceEntryName getResources().getResourceEntryName(resID);

Another way would be to use the setTag() property to assign the name:

v.setTag("BotonJesus");

and get the name by:

String nombre = v.getTag();
    
answered by 05.05.2017 в 00:09
1

To transform an integer value (int) to a String you can use the valueOf function of the String class itself, as indicated by the sample code

public  void BorrarProducto (View v) {
    int resID = v.getId();
    String nombreID = String.valueOf(resID);
    ...
}

Greetings:)

    
answered by 05.05.2017 в 14:16