Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Accessing a global Variable inside an onClickListener
    primarykey
    data
    text
    <p>So in Android/Java, I need to access a global variable that is an <code>EditText[]</code>that I create above the <code>onCreate</code> method of my activity inside of an <code>onClickListener</code>. Here is the variable:</p> <pre><code>public class NCDTCPRelayActivity extends Activity { final EditText[] relayLabelEdits = new EditText[n]; </code></pre> <p>Where <code>n</code> is another global variable integer.</p> <p>My problem is that in order to access these <code>EditText</code> inside an <code>onclickListener</code> i had to make final to access inside the onClickListener, but I need to assign <code>relayLabelEdits</code> to a new <code>EditText[]</code> if the value of n changes which it is wont to do.</p> <p>Below is the <code>onClickListener</code> (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 <code>relayLabelEdits</code> return null unless the <code>relayLabelEdits</code> variable is final):</p> <pre><code> 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 &lt; 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(); } } }); </code></pre> <p>So any ideas on how I can access the <code>EditText[]</code> inside the <code>onClickListener</code> without making it final?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload