Skip to content

Instantly share code, notes, and snippets.

@vinayakakv
Last active February 28, 2017 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinayakakv/9ccbeb73e8bd9f014ad76380a3df5f56 to your computer and use it in GitHub Desktop.
Save vinayakakv/9ccbeb73e8bd9f014ad76380a3df5f56 to your computer and use it in GitHub Desktop.
Use of Lambds!Java 8 <3 :)
//b is a Button
//The format of the function is
//Button.setOnClickListner(View.OnClickListner onClickListner)
//So we are going to implement View.OnClickListner in place
b.setOnClickListener(
//We are going to implement View.OnClickListner interface
//That interface has only one method
//public void onClick(View v)
//takes a View v as input and returns nothing
//Since the interface has only one method, we can use Lambdas and reduce code complexity
//We also need not specify interface name while using Lambdas
//So the syntax of Lambda for particular case becomes
// v -> { /*Do Some Good Thing Here*/ }
//LHS of Lambda operator is function input, RHS is function body
//If the function body is single line of code, we can also omit Curly Braces :)
//In this way, we can make Anonymous implementation of Interface (Containing single function)
v -> new ResultFetchTask //on the button click, we have to start fetching the result
(this, //This is the Context ... Indicates where to show ProgressBar while fetching the result
//We are going to implement interface AsyncTaskCompleteListener<String> here
//That interface has only one method
//public void onTaskComplete(String result)
//This is called by the ResultFetchTask after the completion of excection of Task
//It sends the result as the parameter to this method
//Since the interface has only one mehod, we can use Lambas
//Lambda for particular case looks like
// result -> { /*Show Result*/ }
result -> new AlertDialog.Builder(MainActivity.this) //No need to explain this :P ;) :)
.setMessage(result)
.show()
)
.execute() //Finally I have to execute ResultFetchTask ... Don't forget task
//If the task accepts some input, pass it as paramter to execute Method
//Example assuming usnField is a TextBox (or something) and it is initialized properly
// String usn = usnField.getTest()
// b.setOnClickListner ( #####.execute(usn) )
//Catching the passed parameter is explained in the class of ResultFetchTask
);
// ಇತಿ ಲ್ಯಾಂಬ್ಡಾ ಪುರಾಣ ಕಥಾನಕ ಸಂಪೂರ್ಣಂ
// ಇದನ್ನು ಮನಸ್ಸಿಟ್ಟು ಓದಿದಲ್ಲಿ , ಓದುಗನಿಗೆ ತಂತ್ರಾಂಶ 'ವಿನ್ಯಾಸ' ಸುಲಭವಾಗುವುದು
// ಸಕಲ ಸನ್ಮಂಗಳಾನಿ ಭವಂತು
// :P
// EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment