Amr Ashraf
Amr Ashraf

Reputation: 431

Calling Sequence of Functions From a List at Run Time

I'm making an app to play short pieces of short songs with a specific order based on the user destitution. So to do some thing like that i need to call functions that play the song several times but with different parameters.

So how can i making a list of functions at run time and execute it?

Upvotes: 0

Views: 154

Answers (1)

Lumu Daniel
Lumu Daniel

Reputation: 43

Assuming that the functions follow a sequence (the success of one function, relays the next one in the list), I would suggest creating a callback interface whose methods return booleans. Ensure the methods return a boolean when executed, hence create the ArrayList, make sure the last element in the list is a false regardless.

public void myFunctions(ArrayList<Boolean> functions, CallBackMethods mm){
for (int i = 0; i<functions.size(); i++){
  mm = new FunctionChecker(functions.get(i));
  while(mm.onSuccess()){
    Log.d(TAG,"Function Ran");
  }
}

}

the method above will run all the functions in the arraylist given they individually execute.

below is a class that implements the interface;

public class FunctionChecker implements CallBackMethods{

private boolean methodFlag;

public FunctionChecker(boolean methodFlag) {
    this.methodFlag = methodFlag;
}

@Override
public boolean onSuccess() {
    return methodFlag?methodFlag:onFail();
}

@Override
public boolean onFail() {
    return false;
}

}

and here is the interface;

public interface CallBackMethods {

boolean onSuccess();
boolean onFail();

}

it is a constraining work around but i hope it helps

Upvotes: 1

Related Questions