How can I use if statement of a spinner when the selected value = 0?
if( wilayat.setSelection(0) || city.setSelection(0) ||
station.setSelection(0) || distnation.setSelection(0)) {
message = "Select the 4 fields, please..";
Toast.makeText(BusArrivalTime.this, message, Toast.LENGTH_SHORT).show();
}
Try this.
if (wilayat.getSelectedItemPosition() == 0 || city.getSelectedItemPosition() == 0 || station.getSelectedItemPosition() == 0 || distnation.getSelectedItemPosition() == 0) {
message = "Select the 4 fields, please..";
Toast.makeText(BusArrivalTime.this, message, Toast.LENGTH_SHORT).show();
}
Just use getSelectedItemPosition() method.
You make sure that wilayat、city and so on were Spinner.
Add judgement
if (wilayat != null || city != null || station != null || distnation != null) {
// do something
}
Related
I am writing a code in android for rpn calculations and it crashes when i have operators inside brackets. Eg(89+7) crashes but (8)+7 outputs 15.
This is the method i use for infix to postfix conversion:
private static List<String> convert(String input) {
int p = 0;
String pfb = "";
Stack<Character> chara = new Stack<>();
List<String> pfa = new ArrayList<>();
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
if (pfb.length() > 0) {
pfa.add(pfb);
}
pfb = "";
if (chara.empty()) {
chara.push(ch);
} else {
Character chTop = (Character) chara.peek();
if (chTop == '*' || chTop == '/')
p = 1;
else if (chTop == '+' || chTop == '-')
p = 0;
if (p == 1) {
if (ch == '+' || ch == '-') {
pfa.add(String.valueOf(chara.pop()));
i--;
Log.d("pfa", "" + input.length() + "" + i);
} else { // Same
pfa.add(String.valueOf(chara.pop()));
i--;
}
} else {
if (ch == '+' || ch == '-' && chara.peek() != '(') {
pfa.add(String.valueOf(chara.pop()));
chara.push(ch);
} else
chara.push(ch);
}
}
} else if (ch == '(') {
chara.push(ch);
// Log.d("St",""+chara.peek());
} else if (ch == ')') {
`//Code crashes here` while (chara.peek() != '(' && !chara.empty()) {
pfa.add(String.valueOf(chara.pop()));
}
if (!chara.empty() && chara.peek() != '(')
return null; // invalid expression
else
chara.pop();
} else {
pfb += ch;
}
// Log.d("pfa",""+pfa+"");
}
return pfa;
}
You code crashes because first you peek the stack element and then checking the empty condition.
it should be like
while ( !chara.empty() && chara.peek() != '(' )
I have a list of numbers, in that list I want to check if any of the numbers match, and if so return 'true'. Essentially, what I want to check is if the numbers match, then do not save. I've gotten a seemingly very inefficient method to check. Any help would be appreciated, thanks!
if (mFav1Compare == mNumber1Compare || mFav1Compare == mNumber2Compare || mFav1Compare == mNumber3Compare || mFav1Compare == mNumber4Compare || mFav1Compare == mNumber5Compare) {
return true;
}
if (mFav2Compare == mNumber1Compare || mFav2Compare == mNumber2Compare || mFav2Compare == mNumber3Compare || mFav2Compare == mNumber4Compare || mFav2Compare == mNumber5Compare) {
return true;
}
if (mFav3Compare == mNumber1Compare || mFav3Compare == mNumber2Compare || mFav3Compare == mNumber3Compare || mFav3Compare == mNumber4Compare || mFav3Compare == mNumber5Compare) {
return true;
}
if (mFav4Compare == mNumber1Compare || mFav4Compare == mNumber2Compare || mFav4Compare == mNumber3Compare || mFav4Compare == mNumber4Compare || mFav4Compare == mNumber5Compare) {
return true;
}
if (mFav5Compare == mNumber1Compare || mFav5Compare == mNumber2Compare || mFav5Compare == mNumber3Compare || mFav5Compare == mNumber4Compare || mFav5Compare == mNumber5Compare) {
return true;
}
If you have two lists and you want to know if the second list contains at least one element of the first list you could try
public <T> boolean listMatches(List<T> list1, List<T> list2) {
for (T element : list1) {
if (list2.contains(element)) {
return true;
}
}
return false;
}
Do you thought this?
ArrayList<Integer>listNumbers = new ArrayList<>();
listNumbers.add(99);
listNumbers.add(99);
listNumbers.add(11);
listNumbers.add(10);
for(int i = 0; i < listNumbers.size(); i++){
if(!listCheck(listNumbers.get(i))){
//save something
}
}
and this method
private boolean listCheck(int number){
for(int i = 0; i < listNumbers.size(); i++){
if(number == listNumbers.get(i)){
return true;
}
}
return false;
}
I WANT TO DIRECT IT TO ANOTHER CLASS
if (rbSingle.isChecked() || rbMarried.isChecked()) {
if (sqlName.getText().toString().length() == 0 && sqlcycle.getText().toString().length() == 0
&& sqlperiod.getText().toString().length() == 0 && sqlAge.getText().toString().length() == 0 && tvper.getText().toString().length() == 0) {
sqlName.setError("Please Input Name");
sqlcycle.setError("Please Input your Cycle Length");
sqlperiod.setError("Please Input your Period");
sqlAge.setError("Put your Birthday");
tvper.setError("Put your first day of last period");
} else if (sqlName.getText().toString().length() == 0 && sqlcycle.getText().toString().length() == 0) {
sqlName.setError("Please Input your Cycle Length");
sqlcycle.setError("Please Input your Period");
} else if (sqlName.getText().toString().length() == 0 && sqlperiod.getText().toString().length() == 0) {
sqlName.setError("Please Input your Cycle Length");
sqlperiod.setError("Please Input your Period");
} else if (sqlcycle.getText().toString().length() == 0 && sqlperiod.getText().toString().length() == 0) {
sqlcycle.setError("Please Input your Cycle Length");
sqlperiod.setError("Please Input your Period");
} else if (sqlName.getText().toString().length() == 0) {
sqlName.setError("Please Input your Cycle Length");
} else if (sqlcycle.getText().toString().length() == 0) {
sqlcycle.setError("Please Input your Cycle Length");
} else if (sqlperiod.getText().toString().length() == 0) {
sqlperiod.setError("Please Input your Cycle Length");
}
} else if(sqlName.getText().toString().length() == 0
&& sqlcycle.getText().toString().length() == 0 && sqlperiod.getText().toString().length() == 0) {
Toast.makeText(getApplicationContext(), "Please select your Gender", Toast.LENGTH_SHORT).show();
sqlName.setError("Please Input Name");
sqlcycle.setError("Please Input your Cycle Length");
sqlperiod.setError("Please Input your Period");
}else{
Intent main = new Intent(this, CalendarMain.class);
startActivity(main);
finish();
}
problem: why it wont direct to another page? after the codition i want it to direct to CalendarMain class. can you specify what's the problem in my code?
I made a simpel app that notifies users when they have to check something.
I managed to get the notifications sent with the right delay, but I also want the user to be able to turn off the notification 'bunch' (via a Switch) somewhere between the delays..
I wanted to do this with Handler.removeCallBacks, but it didn't do the job.
final Switch mySwitch = (Switch) findViewById(R.id.theswitch);
mySwitch.setChecked(myPrefs.getBoolean("switch", false));
//attach a listener to check for changes in state
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
final boolean isChecked) {
editor.putBoolean("switch", isChecked);
editor.commit();
if (isChecked) {
//switch has been switched ON
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+1:00"));
Date currentLocalTime = cal.getTime();
DateFormat date = new SimpleDateFormat("HH:mm");
// you can get seconds by adding "...:ss" to it
date.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));
String localTime = date.format(currentLocalTime);
Log.w(localTime, "1");
if (localTime.equals("08:50") || localTime.equals("08:51") || localTime.equals("08:52") || localTime.equals("08:53") || localTime.equals("08:54") || localTime.equals("08:55") || localTime.equals("08:56")
|| localTime.equals("08:57") || localTime.equals("08:58") || localTime.equals("08:59") || localTime.equals("09:00") || localTime.equals("09:01") || localTime.equals("09:02") || localTime.equals("09:03")
|| localTime.equals("09:04") || localTime.equals("09:05") || localTime.equals("09:06") || localTime.equals("09:07") || localTime.equals("09:08") || localTime.equals("09:09") || localTime.equals("09:10")
|| localTime.equals("09:11") || localTime.equals("09:12") || localTime.equals("09:13") || localTime.equals("09:14") || localTime.equals("09:15") || localTime.equals("09:16") || localTime.equals("09:17")
|| localTime.equals("09:18") || localTime.equals("09:19") || localTime.equals("09:20") || localTime.equals("09:21") || localTime.equals("09:22") || localTime.equals("09:23") || localTime.equals("09:24")
|| localTime.equals("09:25") || localTime.equals("09:26") || localTime.equals("09:27") || localTime.equals("09:28") || localTime.equals("09:29") || localTime.equals("09:30") || localTime.equals("09:31")
|| localTime.equals("09:32") || localTime.equals("09:33") || localTime.equals("09:34") || localTime.equals("09:35") || localTime.equals("09:36") || localTime.equals("09:37") || localTime.equals("09:38")
|| localTime.equals("09:39") || localTime.equals("09:40") || localTime.equals("09:41") || localTime.equals("09:42") || localTime.equals("09:43") || localTime.equals("09:44") || localTime.equals("09:45")
|| localTime.equals("09:46") || localTime.equals("09:47") || localTime.equals("09:48") || localTime.equals("09:49") || localTime.equals("09:50") || localTime.equals("09:51") || localTime.equals("09:52")
|| localTime.equals("09:53") || localTime.equals("09:54") || localTime.equals("09:55") || localTime.equals("09:56") || localTime.equals("09:57") || localTime.equals("09:58") || localTime.equals("09:59")
|| localTime.equals("10:00") || localTime.equals("10:01") || localTime.equals("10:02") || localTime.equals("10:03") || localTime.equals("10:04") || localTime.equals("10:05") || localTime.equals("10:06")
|| localTime.equals("10:07") || localTime.equals("10:08") || localTime.equals("10:09") || localTime.equals("10:10") || localTime.equals("14:16")) {
Log.w("It is between", "those times");
boolean beenfired2 = myPrefs.getBoolean("beenfired", false);
if (beenfired2 != true) {
Log.w("yes", "it is not equal to true");
// day 1
mHandler.postDelayed(mClickRunnable, 36000);
mHandler.postDelayed(mClickRunnable, 72000);
beenfired = true;
editor.putBoolean("beenfired", beenfired);
editor.commit();
} else {
Log.w("else", "else");
}
} else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Inlopen.this);
// set title
alertDialogBuilder.setTitle("Alarm kan niet worden ingeschakeld");
// set dialog message
alertDialogBuilder
.setMessage("U kunt het alarm alleen inschakelen tussen 9:00 en 10:00 's ochtends.")
.setCancelable(false)
.setNegativeButton("Oke", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
mySwitch.setChecked(false);
editor.putBoolean("switch", false);
editor.commit();
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
} else {
//switch has been switched OFF
beenfired = false;
mHandler.removeCallbacks(mClickRunnable);
editor.putBoolean("beenfired", beenfired);
editor.commit();
editor.putBoolean("switch", false);
editor.commit();
}
}
});
}
private Handler mHandler = new Handler();
private Runnable mClickRunnable = new Runnable() {
#Override
public void run() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(Inlopen.this)
.setSmallIcon(R.drawable.iconsmall)
.setContentTitle("DM Voet App")
.setContentText("Uw moet uw voeten controleren!");
mBuilder.setAutoCancel(true);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
};
I always combine those two methods when stopping a handler :
mHandler.removeCallbacks(mClickRunnable);
mHandler.removeMessages(0);
To be honest, i dont really understand what is the difference between them, but its work for me. (and it will not work if i remove either one of them)
I was some problem with the if else statement, please its support
if (btn1.getText().toString().equals(myString[6]) &&
(btn1.getText().toString().equals(myString[6]) || (btn1.getText().toString().equals(myString[7]) || (btn1.getText().toString().equals(myString[8]) || (btn1.getText().toString().equals(myString[9]) || (btn1.getText().toString().equals(myString[10]) || (btn1.getText().toString().equals(myString[11]) || (btn1.getText().toString().equals(myString[12]) || (btn1.getText().toString().equals(myString[13]) || (btn1.getText().toString().equals(myString[14]) || (btn1.getText().toString().equals(myString[15])||
(btn2.getText().toString().equals(myString[6]) || (btn2.getText().toString().equals(myString[7]) || (btn2.getText().toString().equals(myString[8]) || (btn2.getText().toString().equals(myString[9]) || (btn2.getText().toString().equals(myString[10]) || (btn2.getText().toString().equals(myString[11]) || (btn2.getText().toString().equals(myString[12]) || (btn2.getText().toString().equals(myString[13]) || (btn2.getText().toString().equals(myString[14]) || (btn2.getText().toString().equals(myString[15])||
(btn3.getText().toString().equals(myString[6]) || (btn3.getText().toString().equals(myString[7]) || (btn3.getText().toString().equals(myString[8]) || (btn3.getText().toString().equals(myString[9]) || (btn3.getText().toString().equals(myString[10]) || (btn3.getText().toString().equals(myString[11]) || (btn3.getText().toString().equals(myString[12]) || (btn3.getText().toString().equals(myString[13]) || (btn3.getText().toString().equals(myString[14]) || (btn3.getText().toString().equals(myString[15])||
(btn4.getText().toString().equals(myString[6]) || (btn4.getText().toString().equals(myString[7]) || (btn4.getText().toString().equals(myString[8]) || (btn4.getText().toString().equals(myString[9]) || (btn4.getText().toString().equals(myString[10]) || (btn4.getText().toString().equals(myString[11]) || (btn4.getText().toString().equals(myString[12]) || (btn4.getText().toString().equals(myString[13]) || (btn4.getText().toString().equals(myString[14]) || (btn4.getText().toString().equals(myString[15])||
(btn5.getText().toString().equals(myString[6]) || (btn5.getText().toString().equals(myString[7]) || (btn5.getText().toString().equals(myString[8]) || (btn5.getText().toString().equals(myString[9]) || (btn5.getText().toString().equals(myString[10]) || (btn5.getText().toString().equals(myString[11]) || (btn5.getText().toString().equals(myString[12]) || (btn5.getText().toString().equals(myString[13]) || (btn5.getText().toString().equals(myString[14]) || (btn5.getText().toString().equals(myString[15])||
(btn01.getText().toString().equals(myString[6]) || (btn01.getText().toString().equals(myString[7]) || (btn01.getText().toString().equals(myString[8]) || (btn01.getText().toString().equals(myString[9]) || (btn01.getText().toString().equals(myString[10]) || (btn01.getText().toString().equals(myString[11]) || (btn01.getText().toString().equals(myString[12]) || (btn01.getText().toString().equals(myString[13]) || (btn01.getText().toString().equals(myString[14]) || (btn01.getText().toString().equals(myString[15])
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
{
Toast.makeText(getBaseContext(), "something wrong", Toast.LENGTH_SHORT).show();
}
else if (btn1.getText().toString() == (btn2.getText().toString())
(btn1.getText().toString() == (btn3.getText().toString())||
(btn1.getText().toString() == (btn4.getText().toString())||
(btn1.getText().toString() == (btn5.getText().toString())||
(btn1.getText().toString() == (btn01.getText().toString())||
(btn2.getText().toString() == (btn3.getText().toString())||
(btn2.getText().toString() == (btn4.getText().toString())||
(btn2.getText().toString() == (btn5.getText().toString())||
(btn2.getText().toString() == (btn01.getText().toString())||
(btn3.getText().toString() == (btn4.getText().toString())||
(btn3.getText().toString() == (btn5.getText().toString())||
(btn3.getText().toString() == (btn01.getText().toString())||
(btn4.getText().toString() == (btn5.getText().toString())||
(btn4.getText().toString() == (btn01.getText().toString())||
(btn5.getText().toString() == (btn01.getText().toString()))))))))))))))))
{
Toast.makeText(getBaseContext(), "something same", Toast.LENGTH_SHORT).show();
}
else (btn1.getText().toString().equals(myString[0])||
(btn1.getText().toString().equals(myString[1])||(btn1.getText().toString().equals(myString[2])||(btn1.getText().toString().equals(myString[3])||(btn1.getText().toString().equals(myString[4])||(btn1.getText().toString().equals(myString[5])))))))
{
Toast.makeText(getBaseContext(), "test", Toast.LENGTH_SHORT).show();
}
}
if the statement is executed that appear only "something same". what should I add from the code above?
first.... else if (btn1.getText().toString() == (btn2.getText().toString())....
Use .equals() to compare String..and use else if ledder instead of one else if then you can check some problems...