Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

android - java events,handlers and listeners question

Edit: I was actually able to get this to work and form, in my oppinion, a good example. The accepted answer is my example and feel free to leave comments if you need assistance or have advice.

I am very new to java and have just been introduced to events, handlers and listeners. I have found 3 good references online, however, i am still not seeing how i can implement this to solve my problem. (Also, this is being developed on the android. I doubt that will change the example, but thought i would let you know)

Links to sites i found helpful:
happy object
example depot
java world

Here is what i need, using arbitrary names and examples instead of plopping my hundreds of lines of code in here:

Here are my class and their uses:
MainUI - this creates the UI
Connect - this class performs a connection to a socket and starts receiving data
DataRobot - this class performs calculations on the data that is received and decides what to do with it
DataBuilder - this class gathers the data from the datarobot and once a certain amount is reached it sends that chunk to a remote database
DataSender - this class sends that data that is sent to it by the databuilder

I was implementing this with threads... =(
Someone i know suggested i use events. He gave me a great explanation, however, it was within 30 minutes and i do not see how i can implement it without seeing some better examples (he was also speaking from a C# background). I was wondering if someone out there with some great experience in java could use what i have above and show me what i need to do.

Here are the events that i have planned out:
dataReceived - this event happens in connect, this event fires when connect receives data when this event fires it sends the data to the data robots handler() function.
dataAnalyzed - this event happens in datarobot, this event fires when datarobot is done handling the data that was given to it, when this event fires it sends the data to the databuilder
dataBuilder - this event happens in databuilder, this event fires when databuilder has received a certain amount and is ready to send the data, when this event fires it sends the data to the datasender.

I am looking for an example here or at least a discussion on what should be done. One of the examples i found was the "happyfaceobject" example and it was pretty good. However, i am still having trouble implementing it in my design, since i am VERY new to java and events. Please assist me when you can and it will be greatly appreciated. This example would also help the community as a whole as there is a lacking of great java events examples.

Thanks in advance, if you need more info let me know.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're very new to Java - I think you're overcomplicating this and trying to swallow too much at once.

Forget about your grand design. Forget about UI. Forget about Android.

Start with a toy problem and build up.

Can you make a single Listener class that responds to an Event and a producer class to send that Event out? Can you see the action being taken? If you can't do that, you won't get far. When you have one working, move onto the next and see if you can get your design working with these simpler objects. Only when the whole thing is working should you worry about tying in the Android and UI elements.

Start with simple POJO models and you'll get further faster.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...