本文介绍了安卓:访问一个onClickListener内的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,在Android的/ Java的,我需要访问一个全局变量,它是一个的EditText [] 我创建上面的的onCreate 的内我的活动的方法 onClickListener 。下面是变量:

 公共类NCDTC prelayActivity延伸活动{
     最终的EditText [] = relayLabelEdits新的EditText [N];

其中, N 是另外一个全局变量整数。

我的问题是,为了访问这些的EditText 内的 onclickListener 我不得不做出最后的访问内在onClickListener,但我需要分配 relayLabelEdits 来一个新的的EditText [] 如果对N的值发生变化,它是惯于做的。

下面是 onClickListener (应用程序的主要部分是打开通过WiFi TCP套接字和控制继电器等等忽略接口的信息以及System.out的。的println点到 relayLabelEdits 返回NULL,除非 relayLabelEdits 变量是决赛):

 按钮保存=新按钮(本);
    save.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            的EditText [] = saveTargets relayLabelEdits;
            的System.out.println(直接:+ relayLabelEdits [0]);
            的System.out.println(第一次尝试+ saveTargets [0]);            //创建一个字符串动态创建的数据库名称
            串relayToSave;            INT myNum的= 0;
            尝试{
                myNum的=的Integer.parseInt(portEditText.getText()的toString());
            }赶上(NumberFormatException的NFE){
                吐司面包= Toast.makeText(getBaseContext(),您输入只能包含数字端口号,Toast.LENGTH_LONG);
                toast.show();
                返回;
            }
            的System.out.println(ipAEditText.getText()的toString());
            cPanel.saveString(IPA,ipAEditText.getText()的toString());
            cPanel.saveInt(端口中,myNum);            的for(int i = 0; I< N;我++){
                relayToSave =接力+(I + 1);
                的System.out.println(我在这里);
                的System.out.println(relayLabelEdits [I]);
                cPanel.saveString(relayToSave,relayLabelEdits [I] .getText()的toString());
                //cPanel.saveString(relayToSave,它的工作);
                的System.out.println(我在这里);
            }            如果(cPanel.connect()==真){
                createMainContainer();
                的setContentView(sView);
                getRelayNames();
                displayRelayNames();
                updateButtonText();
            }
            其他{
                吐司面包= Toast.makeText(getBaseContext(),无法连接,检查IP地址和端口号,Toast.LENGTH_LONG);
                toast.show();
            }
        }
    });

所以,我如何可以访问任何想法的EditText [] 中的 onClickListener 没有使它最终

解决方案

你为什么不使听者类外活动引用传递给它?

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?

解决方案

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

这篇关于安卓:访问一个onClickListener内的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 10:14