So in Android/Java, I need to access a global variable that is an EditText[]
that I create above the onCreate
method of my activity inside of an onClickListener
. Here is the variable:
public class NCDTCPRelayActivity extends Activity {
final EditText[] relayLabelEdits = new EditText[n];
Where n
is another global variable integer.
My problem is that in order to access these EditText
inside an onclickListener
i had to make final to access inside the onClickListener, but I need to assign relayLabelEdits
to a new EditText[]
if the value of n changes which it is wont to do.
Below is the onClickListener
(the main portion of the app is to open a tcp socket and control relays via WiFi so just ignore the socket information and those System.out.println that point to the relayLabelEdits
return null unless the relayLabelEdits
variable is final):
Button save = new Button(this);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText[] saveTargets = relayLabelEdits;
System.out.println("Direct: " + relayLabelEdits[0]);
System.out.println("first try " + saveTargets[0]);
//Creates string to create the database names on the fly
String relayToSave;
int myNum = 0;
try {
myNum = Integer.parseInt(portEditText.getText().toString());
} catch(NumberFormatException nfe) {
Toast toast = Toast.makeText(getBaseContext(), "The Port Number you entered must only contain numbers", Toast.LENGTH_LONG);
toast.show();
return;
}
System.out.println(ipAEditText.getText().toString());
cPanel.saveString("ipA", ipAEditText.getText().toString());
cPanel.saveInt("port", myNum);
for (int i = 0; i < n; i++) {
relayToSave = "relay" + (i+1);
System.out.println("HERE I AM");
System.out.println(relayLabelEdits[i]);
cPanel.saveString(relayToSave, relayLabelEdits[i].getText().toString());
//cPanel.saveString(relayToSave, "It worked");
System.out.println("HERE I AM");
}
if (cPanel.connect() == true){
createMainContainer();
setContentView(sView);
getRelayNames();
displayRelayNames();
updateButtonText();
}
else {
Toast toast = Toast.makeText(getBaseContext(), "Could Not Connect, check IP address and Port Number", Toast.LENGTH_LONG);
toast.show();
}
}
});
So any ideas on how I can access the EditText[]
inside the onClickListener
without making it final?