I'm mostly C# developer and I'm recently working on some Android project. I have to implement some custom written events in Android, but I'm not sure how to do that. I wrote a C# code for what I want to do, so if anyone can help me with translating it into Android code, that would be appreciated.
I need to have a custom function (event), placed at MySecondClass, which can be triggered from MyFirstClass. For example, we have the class:
private class MyFirstClass
{
private event EventHandler<MyCustomEventArgs> _myCustomEvent;
public event EventHandler<MyCustomEventArgs> MyCustomEvent
{
add { _myCustomEvent += value; }
remove { _myCustomEvent -= value; }
}
public void Initialize()
{
MySecondClass myObjectSecondClass = new MySecondClass();
this.MyCustomEvent += myObjectSecondClass.SomeMethodSecondClass;
}
public void SomeMethodFirstClass(int index)
{
//here we will trigger the event with some custom values
EventsHelper.Fire(this.MyCustomEvent, this, new MyCustomEventArgs(index));
}
}
The MyCustomEventArgs is defined as:
public class MyCustomEventArgs : EventArgs
{
public int index;
public MyCustomEventArgs(int indexVal)
{
index = indexVal;
}
}
And the the second class is defined as:
private class MySecondClass
{
public void SomeMethodSecondClass(object sender, MyCustomEventArgs e)
{
//body of the method
//we can use e.index here in the calculations
}
}
So I'm not sure how to handle with these "Event" related commends in Android.