Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to achieve c# event behavior in java (Android)
    primarykey
    data
    text
    <p>when i was programming in c# i was making very often use of events. Now I would like to achieve something similar in Java. I know both languages are diffrent and there are some diffrences in implementing such mechanism. Well to give better picture maybe i will describe a little what i want to get, because i might confuse some of terminology:</p> <p><strong>MyClass</strong> contains code that handles launching <strong>MyEvent</strong> and some code that in certain situations launches that event. </p> <p><strong>MyEventArgs</strong> is class that transports data from <strong>MyClass</strong> and is sent along with event launch, so function that handles <strong>MyEvent</strong> has some additional information about state of <strong>MyClass</strong> instance.</p> <p>And there is <strong>MyApp</strong> class that in main method contains instance of <strong>MyClass</strong> and code handling <strong>MyEvent</strong> and when event is raised inside <strong>MyClass</strong> certain actions are performed in code that listens to <strong>MyEvent</strong>.</p> <p>If it's still unclear, what i mean to achieve is exactly like c# mechanism that is behind button click, just instead of button there is my class, there are my event arguments instead of mouse event args and there is behaviour designed by me instead of click.</p> <p>I've tried to google some answers to my problem and for example i found sites like:</p> <p><a href="http://javarevisited.blogspot.com/2011/12/observer-design-pattern-java-example.html" rel="nofollow">http://javarevisited.blogspot.com/2011/12/observer-design-pattern-java-example.html</a> <a href="http://www.javaworld.com/javaworld/javaqa/2002-03/01-qa-0315-happyevent.html" rel="nofollow">http://www.javaworld.com/javaworld/javaqa/2002-03/01-qa-0315-happyevent.html</a> </p> <p>And either im just lost and I'm looking in bad places/using bad keywords, or what more possible i can't understand anything out of these examples/i can't transform them to work in way I need.</p> <p>What im asking for, is some example, or atleast some draft of code handling such mechanism with use of <strong>MyClass,</strong> <strong>MyEvent,</strong> <strong>MyEventArgs,</strong> names, pointing code i should use in my class to raise event and some sample use of class and handling of event in Main method, so it would help me wrap my mind around this problem.</p> <p><strong>==========Edit========</strong></p> <p>Maybe there are some things like that avaliable for android developers? As my goal is to get to get into mobile apps development after i dust off java.</p> <p><strong>==========Edit========</strong></p> <p>If anyone still interested, here is some sample code, it doesn't use names i metioned, just shows in general what im looking for:</p> <p>And is there no exact solution, maybe somone would suggest something similar to thing im looking for?</p> <p><strong>frmTest.cs</strong></p> <pre><code>namespace SampleApp { public partial class frmTest : Form { CountdownTimer ctmrTest; public frmTest() { InitializeComponent(); ctmrTest = new CountdownTimer(100); ctmrTest.CountdownTimerTick += new CountdownTimer.CountdownTimerTickEventHandler(ctmrTest_CountdownTimerTick); } void ctmrTest_CountdownTimerTick(object sender, CountdownTimerEventArgs ea) { lblTimer.Text = ea.timeString; if (ea.countdownFinished) countdownEnd(); } private void btnStart_Click(object sender, EventArgs e) { ctmrTest.Reset(); ctmrTest.Start(); } void countdownEnd() { MessageBox.Show("Finish"); } } } </code></pre> <p><strong>CountdownTimer.cs</strong></p> <pre><code>namespace SampleApp { public class CountdownTimer { Timer tmrTicks = new Timer(); int secondsLeft = 0; int numberOfSecondsToCountdown = 0; public bool IsWorking { get { return tmrTicks.Enabled; } } public CountdownTimer(int seconds) { ... } void tmrTicks_Tick(object sender, EventArgs e) { ... WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, true)); } public void Reset() { ... WhenCountdownTimerTick(new CountdownTimerEventArgs(secondsLeft, numberOfSecondsToCountdown, false)); } public void Stop() { tmrTicks.Enabled = false; } public void Start() { ,,, } public delegate void CountdownTimerTickEventHandler(object sender, CountdownTimerEventArgs ea); public event CountdownTimerTickEventHandler CountdownTimerTick; protected virtual void WhenCountdownTimerTick(CountdownTimerEventArgs ea) { if (CountdownTimerTick != null) { CountdownTimerTick(this, ea); } } } } </code></pre> <p><strong>CountdownTimerEventArgs.cs</strong></p> <pre><code>namespace SampleApp { public class CountdownTimerEventArgs : EventArgs { public string timeString = ""; public bool countdownFinished = false; public CountdownTimerEventArgs(int secondsLeft, int SecondsToCountdown, bool isfinished) { countdownFinished = isfinished; timeString = string.Format("{0:00}:{1:00}", secondsLeft / 60, secondsLeft % 60); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload