>
> I maybe should have said:
> Can I access attributes/member variables of my activity in my event
> listener? That is: Manipulate the date in my listener and have the updated
> data in my activity?
>

Again, this is a basic Java/Programming question and is not specifically
related to Android in any way...

Still, that being said...

No, you cannot do that in a direct way.  That would violate basic OOP
concepts.  There are two ways you can do this, and they both have their
pros/cons.

One would be to make your listener class an inner/nested class, meaning
that it is defined in the same file and is inside your activity's class
declaration.  It would then have access to your activity's member variables
and methods.  This approach is nice and simple, but not very robust...
Especially if you could envision a scenario where you would want to reuse
the listener with other activities/classes.

If they are in separate files, then you would need to provide a mechanism
for your listener to be able to update values in your activity.  The way I
would do this would be to create an interface with methods like updateX(),
updateY() and so on.  Then have your Activity implement that interface.
Make the constructor take an instance of that interface as a parameter in
its constructor.  Then, in your listener, call the methods on the interface
to update the values in your activity.  This approach is a bit more work,
but is much more scalable.

Thanks,
Justin Anderson
MagouyaWare Developer
http://sites.google.com/site/magouyaware


On Sat, Jun 30, 2012 at 12:54 AM, StSch <[email protected]>wrote:

> I maybe should have said:
> Can I access attributes/member variables of my activity in my event
> listener? That is: Manipulate the date in my listener and have the updated
> data in my activity?
>
> Am Freitag, 29. Juni 2012 21:27:38 UTC+2 schrieb MagouyaWare:
>
>> This is a basic java question, not related to Android in any way...
>>
>> To do that you can create a constructor that takes as parameters the data
>> you want you're listener to have access to.  Alternatively you can add
>> getters/setters to add that data you want separately. ..
>> On Jun 29, 2012 1:21 PM, "StSch" <[email protected]**> wrote:
>>
>>> I have an Activity with a button and register an OnClickListener on this
>>> button that is defined in a separate class file. Can I share data between
>>> these 2 resources? That is: Can I access attributes/member variables from
>>> my activity in my event listener?
>>>
>>> Regards,
>>> -Steffen-
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Android Developers" group.
>>> To post to this group, send email to android-developers@**
>>> googlegroups.com <[email protected]>
>>> To unsubscribe from this group, send email to
>>> android-developers+**[email protected]<android-developers%[email protected]>
>>> For more options, visit this group at
>>> http://groups.google.com/**group/android-developers?hl=en<http://groups.google.com/group/android-developers?hl=en>
>>
>>  --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to