returning from lamda expression - android

in the code shown below how can i return boolean values from this observable (lambda expression)
loginActivityViewModel.checkEmailAndPassword(email,password).observe(this,(response)->{
switch(response){
case LoginActivityViewModel.EMPTY_EMAIL:
handleError(emailWrapper, R.string.error_email_required);
return false;
case LoginActivityViewModel.INVALID_EMAIL:
handleError(emailWrapper, R.string.error_enter_valid_email);
return false;
case LoginActivityViewModel.EMPTY_PASSWORD:
handleError(passwordWrapper, R.string.error_password_required);
return false;
}
});
this block of statement is inside an function that returns boolean values but IDE is telling me unexpected return statement inside the cases.
Thankyou for helping in advance

You are trying to return from within the observable function, not the function that contains your code block. This does not work. Assuming the call on checkEmailAndPassword is the whole point of your method, a better option is to pass a callback function to your method instead of returning a boolean.

You missing default:
loginActivityViewModel.checkEmailAndPassword(email,password).observe(this,(response)->{
switch(response){
case LoginActivityViewModel.EMPTY_EMAIL:
handleError(emailWrapper, R.string.error_email_required);
return false;
case LoginActivityViewModel.INVALID_EMAIL:
handleError(emailWrapper, R.string.error_enter_valid_email);
return false;
case LoginActivityViewModel.EMPTY_PASSWORD:
handleError(passwordWrapper, R.string.error_password_required);
return false;
default:
return false;
}
});

Related

android how to change from if-else to switch case

I have an application about pmt function. However there are so many conditions that need to be handled. Somehow the app will not work with having more than 12 if-else. I want to use switch case, but i still not really understand how to use switch case(been 1 and half month since my 1st try using eclipse).Any example will be highly appreciated.
here is my example code:
if(String1.toString().equals("condition1")){
//do something
if(String2.toString().equals("condition1.1")&& String3.toString().equals("condition1.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition1.##")&& String3.toString().equals("condition1.##")){
//do something else
}
}
else if(String1.toString().equals("condition2")){
//do something
if(String2.toString().equals("condition2.1")&& String3.toString().equals("condition2.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition2.##")&& String3.toString().equals("condition2.##")){
//do something else
}
}
if(String1.toString().equals("condition3")){
//do something
if(String2.toString().equals("condition3.1")&& String3.toString().equals("condition3.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition3.##")&& String3.toString().equals("condition3.##")){
//do something else
}
}
and still keep going....to handle all possibilities .I am wondering, How to do this in switch case . Or a better implementation if we have 3 times 3 conditions. For example a,b,c(suppose these three conditions can only be used once) and d,e,f and g,h,i then condition 1 is a,d,g ; condition 2 is a,d,h condition 3 is a,d,i ; condition 4 a,e,g........on so on
Note:Suppose that the API version is 8-11 (old android)
thanks
The answer is dependent on your target version of android. From KitKat and upwards (API Level 19+), Java 7's switch (String) is available. I'd also strongly suggest trying to group the subcases (condition n.x) into different methods. It just gets very unwieldly quickly, otherwise:
switch (String1.toString) {
case "condition1":
handleCase1(String2, String3);
break;
case "condition2":
handleCase2(String2, String3);
break;
}
If that still results in too complex code, you can try a lookup table together with a command pattern:
class ConditionKey {
final String String1;
final String String2;
final String String3;
public int hashCode(); // hash strings
public boolean equals(); // compare strings
}
interface ConditionCommand {
// use whatever arguments the operation needs, you can also
// add fields and initialize in the constructor
void perform(final ConditionKey key, /* [...] */);
}
Map<ConditionKey, ConditionCommand> actionMap = new HashMap<>();
actionMap.put(
new ConditionKey("condition1", "condition1.1", "condition1.2"),
new ConditionCommand() {
void perform(final ConditionKey key) {
// perform actions that need to be done
}
}
);
And then instead of the if-else or switch-case:
[...]
ConditionKey key = new ConditionKey(string1, string2, string3);
// get the action from the map
ConditionCommand command = actionMap.get(key);
// perform the command
command.perform(key);
since java 1.7 switch on string is supported.
you could annidate two switch:
switch(String1) {
case "condition1": {
switch(String2) {
case "condition1.1":
break;
// ... other cases
default:
break;
}
}
break;
// ... other cases
default break;
}

Android double return

http://imgur.com/DzTRV2D
In android application i have code like below
private boolean isSpinnerNotChoose(Spinner spinner)
{
if(spinner.getSelectedItem().toString().equals(""))
{
return false;
}
return true;
}
But after many tries even if condition is completed it firstly enter on return false and later anyway debbugger goes on return true (If something stays after brackets its skipped but return true always is done. In link is image how its look after if is completed.
Anyone can answer me for that situation ?
Thanks in advance :)
Better use a boolean and return the boolean once.
The boolean is initially set to true.
It will remain true, if no match happens.
If there's a match, it will change to false.
Then the boolean will be returned (true if no match happens, false if there's a match).
No ghost responses.
I also added a trim(), to cut away the eventual trailing spaces.
Try:
private boolean isSpinnerNotChoose(Spinner spinner)
{
boolean myValue = true; // default return value
if(spinner.getSelectedItem().toString().trim().equals(""))
{
myValue = false;
}
return myValue; // return once and for all
}

Syntax error on token "else" delete this token

I can't for the life of me fix this. It is returning an error on else. I tried everything on all the other threads that had the same problem, but it didn't work.
#Override
public boolean onOptionsItemSelected(MenuItem item) { //this method is used for handling menu items' events
// Handle item selection
switch (item.getItemId()) {
case R.id.goBack:
if(myWebView.canGoBack()) {
myWebView.goBack();
}
return true;
else
return super.onOptionsItemSelected(item);{
}
}
}
Eclipse is complaining because the else statement does not follow an if statement -- there's a return true in between (which by the way prevents any code after it from being executed). Fixing your indentation and code formatting helps discover (and also prevent) simple mistakes like this.
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.goBack:
if (myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
}
return super.onOptionsItemSelected(item);
}
The error is because in java (and most-likely any programming language that defines if...else blocks such as c#, c, c++, etc.) the else block (if present) must come right after an if block or an else if statement definition any other statement between the if and else is illegal. However, you have a return statement right before the else block which is illegal and useless because any code blocks after a return statement becomes "unreachable"
You can change your code to...
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.goBack:
if (myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
break;
default:
return super.onOptionsItemSelected(item);
}
}

Android Development: Undefined Method

Hi I´m new to Android and Eclipse. I have just following the tutorial from developer.android.com. Right now I´m in adding ActionBar
Right now I´m at this part
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I have received an error for openSearch() and openSettings(). It said that The method openSettings() is undefined for the type DisplayMessageActivity. What shoud I do now?
Thanks
openSearch() and openSettings() are methods that the author of the tutorial created in order to perform other operations. Search well into the code, there must be somewhere the declaration of those methods, if the author made them visible.
They should look something like this:
public void openSearch() {
//Do something here.
}
public void openSettings() {
//Do something here.
}
Replacing the //Do something here with the code implementation present in the tutorial.
Im up to the same section as you, they haven't provided the methods but you have to implement them as stated above.
However I found code to open up the device settings using this code in the switch;
case R.id.action_settings:
startActivity(new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS));
return true;
define them.
You're basing your code on an incomplete snippet. That snippet makes no expectation of what it means to search or create settings in your app... that's your job to implement. This snippet is only concerned about showing you how to establish the action bar, not the whole application.
The methods openSearch() and openSettings() should be defined. Use the following code. It'd help..
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id){
case R.id.action_search :
startActivity(new Intent(Settings.ACTION_SEARCH_SETTINGS));
return true;
case R.id.action_settings :
startActivity(new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS));
return true;
default :
return super.onOptionsItemSelected(item);
}
}
Maybe you should code those methods?
private void openSearch(){
//your code here
}
private void openSettings(){
//your code here
}
Those two methods are just examples how selecting an option can start an action. The implementation was not provided because it was irrelevant to the example. Note that it is not a tutorial, but a single and un-compile-able example of how to add behavior to an options item.

What is the value of SIM state when "airplane mode" is turned on

I wonder what is the value of SIM state returned by TelephonyManager.getSimState() when "airplane mode" is turned on? This seems to be not directly specified anywhere in the SDK specification.
Actually I need to get SIM operator code (i.e. MCC+MNC) using getSimOperator() method, but JavaDoc states that to use that method:
SIM state must be SIM_STATE_READY
UPDATE
I tested it under emulator and it returns SIM_STATE_UNKNOWN (which is described by javadoc as a "transition between states") after airplane mode is switched on. However I would like to know whether it is a common behavior on Android phones?
After searching Android 4.1 sources I found the following code in one of the private classes com.android.internal.telephony.IccCard:
public State getState() {
if (mState == null) {
switch(mPhone.mCM.getRadioState()) {
/* This switch block must not return anything in
* State.isLocked() or State.ABSENT.
* If it does, handleSimStatus() may break
*/
case RADIO_OFF:
case RADIO_UNAVAILABLE:
case SIM_NOT_READY:
case RUIM_NOT_READY:
return State.UNKNOWN;
case SIM_LOCKED_OR_ABSENT:
case RUIM_LOCKED_OR_ABSENT:
//this should be transient-only
return State.UNKNOWN;
case SIM_READY:
case RUIM_READY:
case NV_READY:
return State.READY;
case NV_NOT_READY:
return State.ABSENT;
}
} else {
return mState;
}
Log.e(mLogTag, "IccCard.getState(): case should never be reached");
return State.UNKNOWN;
}
So State.UNKNOWN would be returned whenever radio state is one of RADIO_OFF or RADIO_UNAVAILABLE. Then State.UNKNOWN will be converted to SIM_STATE_UNKNOWN constant by TelephonyManager.getSimState() method.
As the conclusion: when airplane mode is turned on getSimState will return SIM_STATE_UNKNOWN.
yes, this is the common behavior on android phones.
see the implementation of the getSimState() method from the TelephonyManager class:
public int getSimState() {
String prop = SystemProperties.get(TelephonyProperties.PROPERTY_SIM_STATE);
if ("ABSENT".equals(prop)) {
return SIM_STATE_ABSENT;
}
else if ("PIN_REQUIRED".equals(prop)) {
return SIM_STATE_PIN_REQUIRED;
}
else if ("PUK_REQUIRED".equals(prop)) {
return SIM_STATE_PUK_REQUIRED;
}
else if ("NETWORK_LOCKED".equals(prop)) {
return SIM_STATE_NETWORK_LOCKED;
}
else if ("READY".equals(prop)) {
return SIM_STATE_READY;
}
else {
return SIM_STATE_UNKNOWN;
}
}

Categories

Resources