I have a lot of Buttons that I've created programatically, each button corresponds to a certain action, and I need to be able to identify and retrieve the user input based on which button was clicked. The UI is as follow:
I have like 70 items like those ones.
I've read
Dynamically creating Buttons and setting onClickListener
and
Implementing OnClickListener for dynamically created buttons in Android
But I do not know what is the correct approach. For example, if the user clicks in the Button
Text is now set..., I have to be able to retrieve the value of the EditText
, and know that this amount of minutes corresponds with Andar carrito compra.
Would be correct to assign a tag
to the Button
, in order to identify the index on the screen, and then go up in the View hierarchy and retrieve the EditText
Value?
This is my code so far:
for (int i = 1; i < types.length; i++) {
// ... more views created
// ......
Button enterBt2 = new Button(this);
// Set layout params and stuff for the button
enterBt2.setTag(i);
enterBt2.setOnClickListener(getOnClickDoSomething(enterBt2));
// ....
}
And the OnClickListener
:
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
Log.e(TAG, "O " + ExerciseRecord.TYPE.values()[((int) button.getTag())]);
// Obtain the value of the EditText with
LinearLayout ll = (LinearLayout) button.getParent();
ll.getChildAt(0); //The EditText
}
};
}
Would this be the best way to it?