Skip to content Skip to sidebar Skip to footer

Use Variable Defined In One Event In Other Event

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, i

Solution 1:

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:

publicclassTextextendsActivityimplementsOnClickListener, OnItemClickListener
{}

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

publicvoidonClick(View v){...}
publicvoidonItemClick(AdapterView<?> a, View v, int position, long id){...}

Alternatively you can create another class that implements the Listeners.

publicclassMyListenerimplementsOnClickListener, OnItemClickListener{
    String option;

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

    publicvoidonItemClick(AdapterView<?> a, View v, int position, long id){
        newasyncarbitros().execute(option);
    } 
}

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

MyListenermyListener=newMyListener();
list.setOnItemClickListener(myListener);
btn.setOnClickListener(myListener);

Hope I could help.

Kind regards

Solution 2:

Your text class can implement the listeners directly. For example,

publicclassTextimplementsextendsActivityOnClickListener{
    privateString option;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState){
        yourButton.setOnClickListener(this);
    }

    @OverrideprotectedvoidonClick(View v){
        option = 5;
    }
}

Do the same for on item click listener and you're good. You will be able to use option in your onItemClick method (and likewise for other class/global variables). I will edit a better answer when it's not on my phone :P

Post a Comment for "Use Variable Defined In One Event In Other Event"