Swichcase not working in android? - android

I am stuck in swich case. Please check code below
Log.e("##################### pos",""+posSel_lay);
String ff=Integer.toString(posSel_lay);
//var ffs=Integer.toString(posSel_lay);
Log.e("##################### pos",""+ff);
if(ff.equals("0")){
Log.e("###################Lagan","");
}else if(ff.equals("1")){
Log.e("###################MBBS","");
}else if(ff.equals("2")){
Log.e("###################JODHA","");
}else if(ff.equals("3")){
Log.e("###################ZINDAGI","");
}
I got log only this line
##################### pos 2
& its not going in swich case why i dont know, can you help me please?

All the answers are good.
but you should at least put any message into message param of Log. otherwise you will not able to see this in logs
Log.e("###################ZINDAGI","some message.");

By default a switch case works WITH integers, why don't you try something like the following:
Log.e("##################### pos",""+posSel_lay);
switch (posSel_lay){
case 0:
Log.e("###################Lagan","");
break;
case 1:
Log.e("###################MBBS","");
break;
case 2:
Log.e("###################JODHA","");
break;
case 3:
Log.e("###################ZINDAGI","");
break;
default:
break;
}
Now instead of converting to a string, and using an if-else chain, you use a select statement, which IMO is much cleaner.

Log.e("##################### pos", "" + posSel_lay );
switch( posSel_lay ) {
case 0:
Log.e("###################Lagan","");
break;
case 1:
Log.e("###################MBBS","");
break;
case 2:
Log.e("###################JODHA","");
break;
case 3:
Log.e("###################ZINDAGI","");
break;
}

I can't really understand your question , but here is SwitchCase
switch(posSel_lay){
case 0:
Log.e("###################Lagan","");
break;
case 1:
Log.e("###################MBBS","");
break;
case 2:
Log.e("###################JODHA","");
break;
case 3:
Log.e("###################ZINDAGI","");
break;
}

I found solution. I get this posSel_lay from bundle which is passed by other activity.Previously i written switch code in on button click so now i changed flow,
When i am getting value of posSel_lay from bundle that time only i written code of switch
& there i am making some boolean values true or false which is declared locally. And when user
clicks on button that time i used boolean variables for checking. Then its done.
Thanks for your reply.

Related

How to execute two if conditions in android

I want to execute two if conditions either first if condition or second if condition based on my requirement.
Now below code if it executing second if condition everytime
switch (specilaity_name) {
case "Cardiac":
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_theme);
speclialistimage.setImageResource(R.drawable.ic_heart);
specialization.setTextColor(Color.WHITE);
break;
case "Urology":
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_theme);
speclialistimage.setImageResource(R.drawable.ic_urology__1___1_);
specialization.setTextColor(Color.WHITE);
break;
case "Gynaecology":
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_theme);
speclialistimage.setImageResource(R.drawable.ic_group_2105);
specialization.setTextColor(Color.WHITE);
break;
}
Well. You're using nested if else in the else part of outer if for each block of code.
If only code in second structure is working, then that's the only condition which holds true during execution of your App.
if(specilaity_name.equalsIgnoreCase("Cardiac")){
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_theme);
speclialistimage.setImageResource(R.drawable.ic_heart);
specialization.setTextColor(Color.WHITE);
}else if(specilaity_name.equalsIgnoreCase("Urology")) {
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_white);
speclialistimage.setImageResource(R.drawable.ic_urology__1_);
specialization.setTextColor(Color.rgb(73,179,206));
}else {
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_white);
speclialistimage.setImageResource(R.drawable.ic_group_2105);
specialization.setTextColor(Color.rgb(73, 179, 206));
}
String specialityName = specilaity_name.toUpperCase();
switch (specialityName) {
case "CARDIAC":
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_theme);
speclialistimage.setImageResource(R.drawable.ic_heart);
specialization.setTextColor(Color.WHITE);
break;
case "UROLOGY":
// add code for urology
break;
case "GYNAECOLOGY":
// add code for Gynaecology
break;
default:
break;
}
}

Periodic location report of device using flutter

I need to report the current location of the device using a flutter app. I need to do that continiuesly even when the app is closed.
I currently implemented it using background_fetch which does the job about every 15 minutes.
It works well when the app is open or minimized. But when the app is closed, it functions in Headless mode and doesn't work. the Exception is:
MissingPluginException(No implementation found for method getLocation on channel lyokone/location)
It seems that in Headless mode, not all the app is loaded in memory. I don't have any idea how to solve it.
Also I tried using an Isolate but I face with a new exception:
native function 'Window_sendPlatformMessage' (4 arguments) cannot be found.
Anybody knows how to solve these problems or have any new idea to do the location tracking?
Another option is use package https://github.com/Lyokone/flutterlocation https://github.com/Lyokone/flutterlocation/wiki/Background-Location-Updates
Support To get location updates even your app is closed, https://github.com/Lyokone/flutterlocation/wiki/Background-Location-Updates might have bugs
Also from transistorsoft and provide Android Headless Mod but need license
transistorsoft package https://github.com/transistorsoft/flutter_background_geolocation/wiki/Android-Headless-Mode
Android Headless Mod https://github.com/transistorsoft/flutter_background_geolocation/wiki/Android-Headless-Mode
code snippet
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' as bg;
void headlessTask(bg.HeadlessEvent headlessEvent) async {
print('[BackgroundGeolocation HeadlessTask]: $headlessEvent');
// Implement a 'case' for only those events you're interested in.
switch(headlessEvent.name) {
case bg.Event.TERMINATE:
bg.State state = headlessEvent.event;
print('- State: $state');
break;
case bg.Event.HEARTBEAT:
bg.HeartbeatEvent event = headlessEvent.event;
print('- HeartbeatEvent: $event');
break;
case bg.Event.LOCATION:
bg.Location location = headlessEvent.event;
print('- Location: $location');
break;
case bg.Event.MOTIONCHANGE:
bg.Location location = headlessEvent.event;
print('- Location: $location');
break;
case bg.Event.GEOFENCE:
bg.GeofenceEvent geofenceEvent = headlessEvent.event;
print('- GeofenceEvent: $geofenceEvent');
break;
case bg.Event.GEOFENCESCHANGE:
bg.GeofencesChangeEvent event = headlessEvent.event;
print('- GeofencesChangeEvent: $event');
break;
case bg.Event.SCHEDULE:
bg.State state = headlessEvent.event;
print('- State: $state');
break;
case bg.Event.ACTIVITYCHANGE:
bg.ActivityChangeEvent event = headlessEvent.event;
print('ActivityChangeEvent: $event');
break;
case bg.Event.HTTP:
bg.HttpEvent response = headlessEvent.event;
print('HttpEvent: $response');
break;
case bg.Event.POWERSAVECHANGE:
bool enabled = headlessEvent.event;
print('ProviderChangeEvent: $enabled');
break;
case bg.Event.CONNECTIVITYCHANGE:
bg.ConnectivityChangeEvent event = headlessEvent.event;
print('ConnectivityChangeEvent: $event');
break;
case bg.Event.ENABLEDCHANGE:
bool enabled = headlessEvent.event;
print('EnabledChangeEvent: $enabled');
break;
}
}
void main() {
runApp(HelloWorld());
// Register your headlessTask:
BackgroundGeolocation.registerHeadlessTask(headlessTask);
}

Capturing Multitouch in Android

For my games I allways use singletouch so I allways keep track of the Pointer in an onTouch() Method like this:
-get the pointer Id at (0) with event.getPointerId(0); when pId==0, where pId is the pointer Id
-there will be no computation of other pointers when the pId!=-1(standard value I assign when the prime pointer is lifted up again.) and if the current pointer index is not the same as the pointer indes of the pointer Id I have saved in pId
like so:
switch(e.getAction())
{
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_DOWN:
if(pID!=-1)break;
...
pID=e.getPointerId(0);
...
break;
case MotionEvent.ACTION_MOVE:
if(pID==-1||e.getActionIndex()!=e.findPointerIndex(pID))break;
...
break;
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_UP:
if(pID==-1||e.getActionIndex()!=e.findPointerIndex(pID))break;
...
pID=-1;
...
break;
}
But now I stumbled across a code that I wrote 2 weeks ago where I do it other way, because I wanted to react to Multitouch(maximum two touch pointers allowed for zooming):
switch(e.getAction())
{
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_DOWN:
if(tP1.getPointerIndex()==-1&&tP2.getPointerIndex()==-1)
{
tP1.setPointerIndex(currentPointerIndex);
mode=Mode.SINGLE;
}
else if(tP1.getPointerIndex()!=-1&&tP2.getPointerIndex()==-1&&!isDragging)
{
tP2.setPointerIndex(currentPointerIndex);
mode=Mode.MULTI;
}
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
{
tP1.X=e.getX();
tP1.Y=e.getY();
tP1.oldX=tP1.X;
tP1.oldY=tP1.Y;
}
else if(e.findPointerIndex(tP2.getPointerIndex())==currentPointerIndex)
{
tP2.X=e.getX();
tP2.Y=e.getY();
tP2.oldX=tP2.X;
tP2.oldY=tP2.Y;
}
else break;
...
break;
case MotionEvent.ACTION_MOVE:
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
{
tP1.X=e.getX();
tP1.Y=e.getY();
}
else if(e.findPointerIndex(tP2.getPointerIndex())==currentPointerIndex)
{
tP2.X=e.getX();
tP2.Y=e.getY();
}
else break;
...
break;
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_UP:
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
{
tP1.X=e.getX();
tP1.Y=e.getY();
}
else if(e.findPointerIndex(tP2.getPointerIndex())==currentPointerIndex)
{
tP2.X=e.getX();
tP2.Y=e.getY();
}
else break;
if(mode==Mode.SINGLE)
{
mode=Mode.NONE;
tP1.resetPointerIndex();
}
else if(mode==Mode.MULTI)
{
mode=Mode.SINGLE;
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
{
tP1.resetPointerIndex();
tP1.setPointerIndex(tP2.getPointerIndex());
tP1.X=tP2.X;
tP1.Y=tP2.Y;
tP1.oldX=tP2.oldX;
tP1.oldY=tP2.oldY;
tP2.resetPointerIndex();
}
else if(e.findPointerIndex(tP2.getPointerIndex())==currentPointerIndex)
{
tP2.resetPointerIndex();
}
}
break;
}
tP1 and tP2 ar class objects who keep track of the last x and y coordinates and of the pointer index. So they represent the two pointers. I allways swap the second pointer on the first pointer if the first pointer is raised up and the second is still on. Now it is really embarrassing because I wrote this code for a colleague of mine for a few bucks.
So I jsut keep track of the indices of the pointers but they are sometimes changing. I didnt noticed it because everything was working fine. But now as I came accross it I noticed this. What should I do. Do I need to change it or do you see no problem to let this code be as it is?
I am just assuming that it can go wrong because I dont track the pointer Id but only the pointer Index.
Also I never experience it when testing that for example the primary pointer when first touched get swapped to another index but 0 when another touchpointer comes down.
I feel so dumb that I didnt notice it, so now I need a good advice how to behave pls help...
It is really strange that is code worked because I do this:
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
So Android not only assigns the index of the first pointer with 0 but also the pointerId with 0. This code is a mess i think

OnVideoChatChangeState() usage in Quickblox

I am using quickblox api for 1 to 1 videochat but I dont know the usage OnVideoChatChangeState() of OnQBVideoChatListener() class and with what changes the event is invoked. I have modified the code but the video doesnt start the click functions but doesn't go to:
` public void onVideoChatStateChange(CallState state, VideoChatConfig receivedVideoChatConfig) {
videoChatConfig = receivedVideoChatConfig;
isCanceledVideoCall = false;
Toast.makeText(getApplicationContext(), "switch", Toast.LENGTH_LONG).show();
switch (state)
{
case ON_CALLING:
Toast.makeText(getApplicationContext(), "After this the showCallDialog() will be called.", Toast.LENGTH_LONG).show();
showCallDialog();
break;
case ON_ACCEPT_BY_USER:
progressDialog.dismiss();
startVideoChatActivity();
break;
case ON_REJECTED_BY_USER:
progressDialog.dismiss();
break;
case ON_DID_NOT_ANSWERED:
progressDialog.dismiss();
break;
case ON_CANCELED_CALL:
isCanceledVideoCall = true;
videoChatConfig = null;
break;
case ON_START_CONNECTING:
progressDialog.dismiss();
startVideoChatActivity();
break;
default:
break;
}
}
};
`
and the showCallDialog(); method is not called this shows the events doesn't occur here.
So I want to know can the event occurs so that the methods are called.
This has been fixed. Master branch is updated. Please try download and use the sample once again.

Eclipse Juno/Android broken, the debug is wrong and gen folder not created (R error)

I have a very very strange issue, and i believe my elcipse juno is broken. I have the Android ADT plugin in this eclipse.
The following screenshot will prove my believe.
And its not finished yet. After the screenshot above, i press F6 again, and this is what i got :
I will explain this case once again to make a clear question. I debug my application, then i got that the arg2 value is 1. After that, i press f6 and the code goes to case 0 (instead of case 1) and then i press f6 again and the code goes to case 6 (WTF????? i do have a break, how come it comes to case 0 and case 6?)
In case you need it, this is the code in the above screenshots :
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Intent i = new Intent(this, Chapter.class);
switch(arg2)
{
case 0:
Toast.makeText(this, "a", Toast.LENGTH_SHORT).show();
break;
case 1:
i.putExtra("key", 1);
Toast.makeText(this, arg2 , Toast.LENGTH_SHORT).show();
startActivity(i);
break;
case 2:
i.putExtra("key", 2);
startActivity(i);
break;
case 3:
i.putExtra("key", 3);
startActivity(i);
break;
case 4:
i.putExtra("key", 4);
startActivity(i);
break;
case 5:
i.putExtra("key", 5);
startActivity(i);
break;
case 6:
i.putExtra("key", 6);
startActivity(i);
break;
case 7:
i.putExtra("key", 7);
startActivity(i);
break;
case 8:
i.putExtra("key", 8);
startActivity(i);
break;
}
}
Thank you very much, and any help is appreciated.
I could be completely wrong, but this seems normal to me. The cases work like arrays where the first instance is 0 where your arg2 is an int so the first instance is 1
YES, it is broken because of the bug in ADT 22. I need to upgrade the ADT to latest version and install Android Build Tools. After that, the program and the debugging runs NORMALLY.
Those are the links that may help you if you encounter a same problem with me :
https://groups.google.com/forum/?fromgroups#!topic/android-developers/rCaeT3qckoE
https://groups.google.com/forum/?fromgroups=#!topic/adt-dev/epOfZbKPFdk

Categories

Resources