Android: Accessing a global Variable inside an onClickListener

不打扰是莪最后的温柔 提交于 2019-12-24 07:38:36

问题


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?


回答1:


Why don't you make the listener a class outside and pass the activity reference to it?



来源:https://stackoverflow.com/questions/9117164/android-accessing-a-global-variable-inside-an-onclicklistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!