JAVA 匿名内部类怎么传参给全局变量

 private void startLocation() {
        mLocationClient.start();
        mLocationClient.requestLocation();
        mLocationClient.registerLocationListener(new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation bdLocation) {
                if (bdLocation == null) {
                    return;
                }
                StringBuffer sb = new StringBuffer(256);
                sb.append(bdLocation.getAddrStr());
                mEditLocation.setText(sb.toString());
                StringBuffer sbcity = new StringBuffer(256);
                sbcity.append(bdLocation.getCity());
                StringBuffer code = new StringBuffer(256);
                code.append(bdLocation.getLocType());
                Log.i("error code", code.toString());
                String cityname = sbcity.toString();
                String mcityname = cityname.substring(0, cityname.length() - 1);
                //把汉语转化为拼音
                String mycityname=ChineseToPinYin.getPinYin(mcityname);
                //根据位置获取天气
                getWeather(mycityname);
                mLocationClient.stop();

                city=mycityname;
            }
        });
    }

最后的city是我定义的 全局变量,但是这样传参 全局变量是得不到值的 ,这是在监听器的匿名内部类里传参给全局变量 该怎么办

mEditLocation这个变量可以使用,你可以按照定义mEditLocation的方式来定义city,如果还是不行,你可以定义一个和startLocation相似的方法,以String类型作为参数,在city=mycityname;的地方调用这个方法,在方法里给city赋值

 public class A {
    String city = "";
   public void setCity(String city){
       this.city = city;
   }
   public void set(){
       new KInterface(){

        @Override
        public void test() {
            String mycityname = "";
            setCity(mycityname);//方式1
            A.this.city = mycityname;//方式2
        }          
       };
   }

}
如果有帮助,记得采纳一下哦

把city设置为外部类的属性试试看。

city 给city生成getCity和setCity方法 赋值是 类.setCity(mycityname)

http://ldzyz007.iteye.com/blog/844380

http://zhidao.baidu.com/link?url=TVleQUt04DZzDyz5A3cbBWanprnh2jtrfKLNsyzdUUTG56F3icsYF9XP7NJcb6SkIrXXBvH7Jzp91mEZ1UoKbSOifDmVhLOLmyiGr4xHBo_

是不是你添加的listener没有触发啊?比如我这个例子,是可以修改,关键是要触发你自己注册的按个listener。


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

public class InnerClassDemo {
    String city = "shanghai";
    private JButton btn;

    public InnerClassDemo() {
        btn = new JButton("测试");
    }

    public void addActionListener() {
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("按钮被按下");
                System.out.println("原来city的值为:" + city);
                city = "beijing";
            }
        });
    }

    public static void main(String[] args) {
        InnerClassDemo a = new InnerClassDemo();
        a.addActionListener();
        a.btn.doClick();
        System.out.println("现在city的值为:"+a.city);
    }
}

输出结果:

按钮被按下
原来city的值为:shanghai
现在city的值为:beijing

city 给city生成getCity和setCity方法 赋值是 类.setCity(mycityname)