Hi,
I'm kinda new to Java but was getting along fine until now.
I'm working on an Android game using the Android SDK, but I think my question is purely Java related, so I don't think that's important.
What I want to do is quite simple but might be hard to explain. I want to be able to pass a "method" as a parameter into a class, and invoke or call that method at a later time.
I have a class that loads some data over the internet, which might take a few seconds. Obviously I am loading the data asynchronously in a separate thread to not lock op the UI during that time. Android has a couple useful classes just for that thing, but they are a little tedious to use, so I'm looking for another way.
The ideal solution I am looking for would be a class that looks like this:
I want to pass two methods:Code:public class GetDataTask<E> extends (some Android class) { private ???<Void, E> getDataMethod; private ???<E, Void> finishedMethod; public GetDataTask(??? method1, ??? method2) { getDataMethod = method1; finishedMethod = method2; } @Override public E doInBackground() { // This method is called by the superclass and runs in a background thread // call the method passed as the getDataMethod return getDataMethod.invoke(); } @Override public void onPostExecute(E value) { // This method is called by superclass as soon as the background thread method has completed // call the finished method finishedMethod.invoke(value); } }
- getDataMethod is a method that returns an object of type E (the generic type of the class)
- finishedMethod is a method that accepts an object of type E.
Usage would then be very simple, for example:
The loadUser and loadUserFinished methods are the two methods I pass along to my GetDataTask.Code:public void onSomeButtonClicked() { // Get user details: GetDataTask<User> task = new GetDataTask<User>(loadUser, loadUserFinished); task.execute(); } public User loadUser() { return Networking.getUserFromNetwork(); } public void loadUserFinished(User user) { txtUserName.setText(user.username); }
I have no clue what to use as the ??? types. I am now thinking in .NET terms where these methods could be passed as Action<T>, Func<Tin, Tout>, etc. As far as I know, there is nothing like this in Java.
One option I thought of was that the ??? types would be an Interface that my main window (Activity actually in Android) has to implement.
For example, in this case:
Then I can let my window implement IDataLoader<User, Void> and it would have the getData and getDataFinished methods. Instead of passing these two methods, I pass the entire window (as an IDataLoader reference) and I can then call these methods on it.Code:public interface IDataLoader<E1, E2> { E1 getData(E2); E2 getDataFinished(E1); }
There is one major problem here though: it works fine if I just have one of this task, but if I have another task, let's say one that returns Messages sent by the user, then I have to implement the same interface for that task. That apparently is impossible in Java, I cannot implement the same interface twice with different type arguments. So that doesn't seem like an option...
So what are my options?
Another usage I can think of is using anonymous inner classes (I think), but I have no clue how I would implement that in my GetDataTask:
Again, a couple of ??? types where I don't know what to use. Also, as far as I can see I now have no access to the txtUserName, because this onFinished method in the loadUserFinished 'field' (what is it? inner type?) doesn't know about the class it's in.. right?Code:public void onSomeButtonClicked() { // Get user details: GetDataTask<User> task = new GetDataTask<User>(loadUser, loadUserFinished); task.execute(); } private loadUser = new ??? () { public User onLoad() { return Networking.getUserFromNetwork(); } } private loadUserFinished = new ??? () { public void onFinished(User user) { // Get reference to the textbox, dunno how txtUserName.setText(user.username); } }
So what are my options, how can I do what I want to do here?
Thanks for any help!




Reply With Quote