使用在其它事件一个事件定义的变量 [英] Use variable defined in one event in other event

查看:137
本文介绍了使用在其它事件一个事件定义的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量在类的开始申报。里面的onCreate方法,我有两个监听器。在一个我从一个ListView一个值获得,并将其分配给变量。然后,在其他的听众,我需要使用这个变量,但它是空的。我该如何解决呢?谢谢你。

 公共类文本扩展活动{
    字符串选项;
    按钮BTN;
    ListView控件列表;
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_telefonos);
        BTN =(按钮)findViewById(R.id.btn);
        名单=(ListView控件)findViewById(R.id.list);
        list.setOnItemClickListener(新OnItemClickListener(){
            公共无效onItemClick(适配器视图<>一种,视图V,INT位置,长的id){
                选项​​=((Arbitros)a.getAdapter()的getItem(位置)。)getNombre();
            }
        });
        btn.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                新asyncarbitros()执行(选件);
            }
        });
}


解决方案

您有几个不同的possibilites。一个最简单的将是(如@ zgc7009评论)给两个接口(OnClickListener和OnItemClickListener)落实到你的活动。

:这可以通过简单地改变你的类声明下面的行来完成

 公共类文本扩展活动实现OnClickListener,OnItemClickListener
{}

在这种情况下,你将必须实现以下两种方法:

 公共无效的onClick(视图v){...}
公共无效onItemClick(适配器视图<>一种,视图V,INT位置,长的id){...}

另外,您可以创建一个实现监听器的类。

 公共类myListener的实现OnClickListener,OnItemClickListener {
    字符串选项;    公共无效的onClick(视图v){
        选项​​=((Arbitros)a.getAdapter()的getItem(位置)。)getNombre();
    }    公共无效onItemClick(适配器视图<>一种,视图V,INT位置,长的id){
        新asyncarbitros()执行(选件);
    }
}

和你将不得不到监听器添加到您的两个视图。

  myListener的myListener的=新myListener的();
list.setOnItemClickListener(myListener的);
btn.setOnClickListener(myListener的);

希望我能有所帮助。

亲切的问候

I have one variable declare at the start of the class. Inside the onCreate method I have two Listener. In one I get from a listview one value and assign it to the variable. Then, in the other listener I need to use this variable but it's empty. How can I solve it? Thank you.

public class Text extends Activity{
    String option;
    Button btn;
    ListView list;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_telefonos);
        btn=(Button)findViewById(R.id.btn);
        list=(ListView)findViewById(R.id.list);
        list.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView<?> a, View v, int position, long id){
                option=((Arbitros)a.getAdapter().getItem(position)).getNombre();
            }
        });
        btn.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                new asyncarbitros().execute(option);
            }
        });
}

解决方案

You have several different possibilites. The easiest one would be (as @zgc7009 commented) to implement both interfaces (OnClickListener and OnItemClickListener) into your activity. This can be done by simply changing your class declaration to the following line:

public class Text extends Activity implements OnClickListener, OnItemClickListener
{}

In that case you will have to implement the following two methods:

public void onClick(View v){...}
public void onItemClick(AdapterView<?> a, View v, int position, long id){...}

Alternatively you can create another class that implements the Listeners.

public class MyListener implements OnClickListener, OnItemClickListener{
    String option;

    public void onClick(View v){
        option=((Arbitros)a.getAdapter().getItem(position)).getNombre();
    }

    public void onItemClick(AdapterView<?> a, View v, int position, long id){
        new asyncarbitros().execute(option);
    } 
}

And you would have to add that Listener to your two Views.

MyListener myListener = new MyListener();
list.setOnItemClickListener(myListener);
btn.setOnClickListener(myListener);

Hope I could help.

Kind regards

这篇关于使用在其它事件一个事件定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆