在Android中的onItemClick中访问变量

时间:2015-09-10 22:41:26

标签: android variables listener

我在访问<div class="container"> <h2>LED Power Supply Calculator</h2> <p>Please select your LED type, then type how many LEDs you will be using in your box</p> <div class="dropdown"> <button class="btn btn-default dropdown-toggle" type="button" id="menu1" data-toggle="dropdown">Select your LED <span class="caret"></span></button> <ul class="dropdown-menu" role="menu" aria-labelledby="menu1"> <li class="dropdown-header">Wide angle beam</li> <li role="presentation"><a role="menuitem" tabindex="-1" onclick="w(1.2);">HLC3S - HW W65K</a></li> <li role="presentation"><a role="menuitem" tabindex="-1" onclick="w(0.72);">HLC3S - LW W65K</a></li> <li role="presentation" class="divider"></li> <li class="dropdown-header">Standard beam</li> <li role="presentation"><a role="menuitem" tabindex="-1" onclick="w(0.06);">HLC3S XD W65K</a></li> </ul> </div> <BR> <form role="form"> <div class="form-group"> <label for="usr">How many LEDs</label> <input type="number" class="form-control" id="usr"> <span class="form-control" id="watt"></span> </div> </div> <script type="text/javascript"> function w(val){ document.getElementById('watt').innerHTML = val*10; } //document.write(w * 10); </script> 内修改的变量时遇到问题。这就是我的代码现在的样子:

onItemClick

public String parseXML(ArrayList<String> XML) throws IOException, ParserConfigurationException, SAXException, JSONException { ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainMenu.this,android.R.layout.select_dialog_singlechoice, XML); AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.outputcities); acTextView.setThreshold(1); acTextView.setAdapter(adapter); acTextView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { result = parent.getItemAtPosition(position).toString(); } }); TextView yo = (TextView)findViewById(R.id.sample); yo.setText(getResult()); return result; } 目前只输出TextView,因为我将其初始化为最顶层(作为全局变量)。无论如何我可以访问""内部已更改的变量吗?我尝试静态地添加一个set和get方法,但即使这样也没有产生我想要的结果。如果您需要更多说明/代码,请告诉我。

谢谢!

2 个答案:

答案 0 :(得分:1)

onItemClick内的代码在parseXML方法的其余部分之后运行,因为它是Listener,并且在方法调用期间代码不会运行。假设result是一个类变量,请将您的方法更改为:

public void parseXML(ArrayList<String> XML) throws IOException, ParserConfigurationException, SAXException, JSONException
 {
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainMenu.this,android.R.layout.select_dialog_singlechoice, XML);
     AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.outputcities);
     acTextView.setThreshold(1);
     acTextView.setAdapter(adapter);

     acTextView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            result = parent.getItemAtPosition(position).toString();
            processResult();
        }
    });
 }

public void processResult() {
    TextView yo = (TextView) findViewById(R.id.sample);
    yo.setText(result);
    // Do the rest of your stuff here
    // that you were trying to do with the return value of parseXML
}

答案 1 :(得分:0)

尝试将结果变量声明为您的类的全局变量,并且您可以在clickListener中获取访问权。