error with if else statement - android

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...

Related

selected item of a spinner in an if statement

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
}

Compare list of numbers to see if they match in Android

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;
}

progress bar not working webview android

i am trying to implement progressbar in my webview but its not showing any progress i am getting this warning "The type MainActivity.MyWebViewClient is never used locally" in the line
private class MyWebViewClient extends WebChromeClient {
CODE
package com.abc.browser;
import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
public class MainActivity extends Activity {
private WebView mWebView;
private EditText urlEditText;
private ProgressBar progress;
// FULL-SCREEN VIDEO-1
private LinearLayout mContentView;
private FrameLayout mCustomViewContainer;
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
private WebChromeClient mWebChromeClient;
// FULL-SCREEN VIDEO-1
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.menu2).setVisibility(View.GONE);
findViewById(R.id.set_1_b).setVisibility(View.GONE);
findViewById(R.id.set_2_b).setVisibility(View.GONE);
findViewById(R.id.reload_btn).setVisibility(View.GONE);
findViewById(R.id.menu3).setVisibility(View.GONE);
findViewById(R.id.menu1).setVisibility(View.GONE);
// FULL-SCREEN VIDEO-2
mContentView = (LinearLayout) findViewById(R.id.linearlayout);
mWebView = (WebView) findViewById(R.id.webView);
mCustomViewContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content);
mWebChromeClient = new WebChromeClient() {
#Override
public void onShowCustomView(View view,
WebChromeClient.CustomViewCallback callback) {
// if a view already exists then immediately terminate the new
// one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
// Add the custom view to its container.
mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
mCustomView = view;
mCustomViewCallback = callback;
// hide main browser view
mContentView.setVisibility(View.GONE);
// Finally show the custom view container.
mCustomViewContainer.setVisibility(View.VISIBLE);
mCustomViewContainer.bringToFront();
}
#Override
public void onHideCustomView() {
if (mCustomView == null)
return;
// Hide the custom view.
mCustomView.setVisibility(View.GONE);
// Remove the custom view from its container.
mCustomViewContainer.removeView(mCustomView);
mCustomView = null;
mCustomViewContainer.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
// Show the content view.
mContentView.setVisibility(View.VISIBLE);
}
};
// FULL-SCREEN VIDEO-2
WebSettings webSettings = mWebView.getSettings();
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new HelloWebViewClient());
// Download manager
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Request request = new Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, "download");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
// **//
// URL BAR AND PROGRESS BAR
progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setMax(100);
urlEditText = (EditText) findViewById(R.id.urlField);
// Use Done/search button in keyboard as go button
urlEditText.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
// Toast.makeText(getApplicationContext(), "some key pressed",
// Toast.LENGTH_LONG).show();
String url = urlEditText.getText().toString();
if (url.endsWith(".ac") || url.endsWith(".ac.uk")
|| url.endsWith(".ad") || url.endsWith(".ae")
|| url.endsWith(".aero") || url.endsWith(".af")
|| url.endsWith(".ag") || url.endsWith(".ai")
|| url.endsWith(".al") || url.endsWith(".am")
|| url.endsWith(".an") || url.endsWith(".ao")
|| url.endsWith(".aq") || url.endsWith(".ar")
|| url.endsWith(".arpa") || url.endsWith(".as")
|| url.endsWith(".asia") || url.endsWith(".at")
|| url.endsWith(".au") || url.endsWith(".aw")
|| url.endsWith(".ax") || url.endsWith(".az")
|| url.endsWith(".ba") || url.endsWith(".bb")
|| url.endsWith(".bd") || url.endsWith(".be")
|| url.endsWith(".bf") || url.endsWith(".bg")
|| url.endsWith(".bh") || url.endsWith(".bi")
|| url.endsWith(".biz") || url.endsWith(".bj")
|| url.endsWith(".bm") || url.endsWith(".bn")
|| url.endsWith(".bo") || url.endsWith(".br")
|| url.endsWith(".bs") || url.endsWith(".bt")
|| url.endsWith(".bv") || url.endsWith(".bw")
|| url.endsWith(".by") || url.endsWith(".bz")
|| url.endsWith(".ca") || url.endsWith(".cat")
|| url.endsWith(".cc") || url.endsWith(".cd")
|| url.endsWith(".cf") || url.endsWith(".cg")
|| url.endsWith(".ch") || url.endsWith(".ci")
|| url.endsWith(".ck") || url.endsWith(".cl")
|| url.endsWith(".cm") || url.endsWith(".cn")
|| url.endsWith(".co") || url.endsWith(".co.uk")
|| url.endsWith(".com") || url.endsWith(".coop")
|| url.endsWith(".cr") || url.endsWith(".cs")
|| url.endsWith(".cu") || url.endsWith(".cv")
|| url.endsWith(".cx") || url.endsWith(".cy")
|| url.endsWith(".cz") || url.endsWith(".dd")
|| url.endsWith(".de") || url.endsWith(".dj")
|| url.endsWith(".dk") || url.endsWith(".dm")
|| url.endsWith(".do") || url.endsWith(".dz")
|| url.endsWith(".ec") || url.endsWith(".edu")
|| url.endsWith(".ee") || url.endsWith(".eg")
|| url.endsWith(".eh") || url.endsWith(".er")
|| url.endsWith(".es") || url.endsWith(".et")
|| url.endsWith(".eu") || url.endsWith(".fi")
|| url.endsWith(".firm") || url.endsWith(".fj")
|| url.endsWith(".fk") || url.endsWith(".fm")
|| url.endsWith(".fo") || url.endsWith(".fr")
|| url.endsWith(".fx.ga") || url.endsWith(".gb")
|| url.endsWith(".gd") || url.endsWith(".ge")
|| url.endsWith(".gf") || url.endsWith(".gh")
|| url.endsWith(".gi") || url.endsWith(".gl")
|| url.endsWith(".gm") || url.endsWith(".gn")
|| url.endsWith(".gov") || url.endsWith(".gov.uk")
|| url.endsWith(".gp") || url.endsWith(".gq")
|| url.endsWith(".gr") || url.endsWith(".gs")
|| url.endsWith(".gt") || url.endsWith(".gu")
|| url.endsWith(".gw") || url.endsWith(".gy")
|| url.endsWith(".hk") || url.endsWith(".hm")
|| url.endsWith(".hn") || url.endsWith(".hr")
|| url.endsWith(".ht") || url.endsWith(".hu")
|| url.endsWith(".id") || url.endsWith(".ie")
|| url.endsWith(".il") || url.endsWith(".im")
|| url.endsWith(".in") || url.endsWith(".info")
|| url.endsWith(".int") || url.endsWith(".io")
|| url.endsWith(".iq") || url.endsWith(".ir.is")
|| url.endsWith(".it") || url.endsWith(".je")
|| url.endsWith(".jm") || url.endsWith(".jo")
|| url.endsWith(".jobs") || url.endsWith(".jp")
|| url.endsWith(".ke") || url.endsWith(".kg")
|| url.endsWith(".kh") || url.endsWith(".ki")
|| url.endsWith(".km") || url.endsWith(".kn")
|| url.endsWith(".kp") || url.endsWith(".kr")
|| url.endsWith(".kw") || url.endsWith(".ky")
|| url.endsWith(".kz") || url.endsWith(".la")
|| url.endsWith(".lb") || url.endsWith(".lc")
|| url.endsWith(".li") || url.endsWith(".lk")
|| url.endsWith(".lr") || url.endsWith(".ls")
|| url.endsWith(".lt") || url.endsWith(".ltd.uk")
|| url.endsWith(".lu") || url.endsWith(".lv")
|| url.endsWith(".ly") || url.endsWith(".ma")
|| url.endsWith(".mc") || url.endsWith(".md")
|| url.endsWith(".me") || url.endsWith(".me.uk")
|| url.endsWith(".mg") || url.endsWith(".mh")
|| url.endsWith(".mil") || url.endsWith(".mk")
|| url.endsWith(".ml") || url.endsWith(".mm")
|| url.endsWith(".mn") || url.endsWith(".mo")
|| url.endsWith(".mobi") || url.endsWith(".mod.uk")
|| url.endsWith(".mp") || url.endsWith(".mq")
|| url.endsWith(".mr") || url.endsWith(".ms")
|| url.endsWith(".mt") || url.endsWith(".mu")
|| url.endsWith(".museum") || url.endsWith(".mv")
|| url.endsWith(".mw") || url.endsWith(".mx")
|| url.endsWith(".my") || url.endsWith(".mz.na")
|| url.endsWith(".name") || url.endsWith(".nato")
|| url.endsWith(".nc") || url.endsWith(".ne")
|| url.endsWith(".net") || url.endsWith(".net.uk")
|| url.endsWith(".nf") || url.endsWith(".ng")
|| url.endsWith(".nhs.uk") || url.endsWith(".ni")
|| url.endsWith(".nl") || url.endsWith(".no")
|| url.endsWith(".nom") || url.endsWith(".np")
|| url.endsWith(".nr") || url.endsWith(".nt")
|| url.endsWith(".nu") || url.endsWith(".nz")
|| url.endsWith(".om") || url.endsWith(".org")
|| url.endsWith(".org.uk") || url.endsWith(".pa")
|| url.endsWith(".pe") || url.endsWith(".pf")
|| url.endsWith(".pg") || url.endsWith(".ph")
|| url.endsWith(".pk") || url.endsWith(".pl")
|| url.endsWith(".plc.uk") || url.endsWith(".pm")
|| url.endsWith(".pn") || url.endsWith(".post")
|| url.endsWith(".pr") || url.endsWith(".pro")
|| url.endsWith(".ps") || url.endsWith(".pt")
|| url.endsWith(".pw") || url.endsWith(".py")
|| url.endsWith(".qa") || url.endsWith(".re")
|| url.endsWith(".ro") || url.endsWith(".ru")
|| url.endsWith(".rw") || url.endsWith(".sa")
|| url.endsWith(".sb") || url.endsWith(".sc")
|| url.endsWith(".sch.uk") || url.endsWith(".sd")
|| url.endsWith(".se") || url.endsWith(".sg")
|| url.endsWith(".sh") || url.endsWith(".si")
|| url.endsWith(".sj") || url.endsWith(".sk")
|| url.endsWith(".sl") || url.endsWith(".sm")
|| url.endsWith(".sn") || url.endsWith(".so")
|| url.endsWith(".sr") || url.endsWith(".store")
|| url.endsWith(".su") || url.endsWith(".sv")
|| url.endsWith(".sy") || url.endsWith(".sz")
|| url.endsWith(".tc") || url.endsWith(".td")
|| url.endsWith(".tel") || url.endsWith(".tf")
|| url.endsWith(".tg") || url.endsWith(".th")
|| url.endsWith(".tj") || url.endsWith(".tk")
|| url.endsWith(".tl") || url.endsWith(".tm")
|| url.endsWith(".tn") || url.endsWith(".to")
|| url.endsWith(".tp") || url.endsWith(".tr")
|| url.endsWith(".travel") || url.endsWith(".tt")
|| url.endsWith(".tv") || url.endsWith(".tw")
|| url.endsWith(".tz") || url.endsWith(".ua")
|| url.endsWith(".ug") || url.endsWith(".uk")
|| url.endsWith(".um") || url.endsWith(".us")
|| url.endsWith(".uy") || url.endsWith(".va")
|| url.endsWith(".vc") || url.endsWith(".ve")
|| url.endsWith(".vg") || url.endsWith(".vi")
|| url.endsWith(".vn") || url.endsWith(".vu")
|| url.endsWith(".web") || url.endsWith(".wf")
|| url.endsWith(".ws") || url.endsWith(".xxx")
|| url.endsWith(".ye") || url.endsWith(".yt")
|| url.endsWith(".yu") || url.endsWith(".za")
|| url.endsWith(".zm") || url.endsWith(".zr")
|| url.endsWith(".zw")) {
if (!url.startsWith("http://")
&& !url.startsWith("https://")) {
url = "http://" + url;
}
} else
url = "https://www.google.com/search?q="
+ url.replace(" ", "+");// Prefix (replace spaces
// with a '+' sign)
mWebView.loadUrl(url);
if (validateUrl(url)) {
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
MainActivity.this.progress.setProgress(10);
}
return false;
}
private boolean validateUrl(String url) {
return true;
}
});
// **//
}
private class MyWebViewClient extends WebChromeClient {
#Override
public void onProgressChanged(WebView view, int newProgress) {
MainActivity.this.setValue(newProgress);
super.onProgressChanged(view, newProgress);
}
}
public void setValue(int progress) {
this.progress.setProgress(progress);
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url) {
webview.setWebChromeClient(mWebChromeClient);
webview.loadUrl(url);
return true;
}
}
// FULL-SCREEN VIDEO-3
#Override
protected void onStop() {// plays video outside frame on back button and
// prevents from going back
super.onStop();
if (mCustomView != null) {
if (mCustomViewCallback != null)
mCustomViewCallback.onCustomViewHidden();
mCustomView = null;
}
}
#Override
public void onBackPressed() {// hide custom view (in which the video plays)
// on back button
super.onBackPressed();
if (mCustomView != null) {
mWebChromeClient.onHideCustomView();
} else {
finish();
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {// prevents the
// custom view from
// running in
// background when
// app is closed
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
// FULL-SCREEN VIDEO-3
}
}

Handler not canceling Runnables (Android)

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)

Better way to handle if statments for multiple text views?

I have some textviews which I want to check them and return the first which is empty. I've managed to do it this way:
public TextView CurrentPos(){
if (!wordLetter1.getText().toString().equals("*") &&
!wordLetter2.getText().toString().equals("*") &&
!wordLetter3.getText().toString().equals("*") &&
!wordLetter4.getText().toString().equals("*") &&
!wordLetter5.getText().toString().equals("*") &&
!wordLetter6.getText().toString().equals("*") &&
!wordLetter7.getText().toString().equals("*")){
if (wordLetter1.getText().toString().equals("") &&
wordLetter2.getText().toString().equals("") &&
wordLetter3.getText().toString().equals("") &&
wordLetter4.getText().toString().equals("") &&
wordLetter5.getText().toString().equals("") &&
wordLetter6.getText().toString().equals("") &&
wordLetter7.getText().toString().equals(""))
return wordLetter1;
else if (!wordLetter1.getText().toString().equals("") &&
wordLetter2.getText().toString().equals("") &&
wordLetter3.getText().toString().equals("") &&
wordLetter4.getText().toString().equals("") &&
wordLetter5.getText().toString().equals("") &&
wordLetter6.getText().toString().equals("") &&
wordLetter7.getText().toString().equals(""))
return wordLetter2;
else if (!wordLetter1.getText().toString().equals("") &&
!wordLetter2.getText().toString().equals("") &&
wordLetter3.getText().toString().equals("") &&
wordLetter4.getText().toString().equals("") &&
wordLetter5.getText().toString().equals("") &&
wordLetter6.getText().toString().equals("") &&
wordLetter7.getText().toString().equals(""))
return wordLetter3;
else if (!wordLetter1.getText().toString().equals("") &&
!wordLetter2.getText().toString().equals("") &&
!wordLetter3.getText().toString().equals("") &&
wordLetter4.getText().toString().equals("") &&
wordLetter5.getText().toString().equals("") &&
wordLetter6.getText().toString().equals("") &&
wordLetter7.getText().toString().equals(""))
return wordLetter4;
else if (!wordLetter1.getText().toString().equals("") &&
!wordLetter2.getText().toString().equals("") &&
!wordLetter3.getText().toString().equals("") &&
!wordLetter4.getText().toString().equals("") &&
wordLetter5.getText().toString().equals("") &&
wordLetter6.getText().toString().equals("") &&
wordLetter7.getText().toString().equals(""))
return wordLetter5;
else if (!wordLetter1.getText().toString().equals("") &&
!wordLetter2.getText().toString().equals("") &&
!wordLetter3.getText().toString().equals("") &&
!wordLetter4.getText().toString().equals("") &&
!wordLetter5.getText().toString().equals("") &&
wordLetter6.getText().toString().equals("") &&
wordLetter7.getText().toString().equals(""))
return wordLetter6;
else if (!wordLetter1.getText().toString().equals("") &&
!wordLetter2.getText().toString().equals("") &&
!wordLetter3.getText().toString().equals("") &&
!wordLetter4.getText().toString().equals("") &&
!wordLetter5.getText().toString().equals("") &&
!wordLetter6.getText().toString().equals("") &&
wordLetter7.getText().toString().equals(""))
return wordLetter7;
}
return null;
}
The above works fine, but I think that must be a more compat way to achive the same with a less lines of code. I haven't much experience on java and I found some answers for similar situations, which I cannot fully understand.
I tried to reduse my code for CurrentPos() method with the above code:
public TextView CurrentPos(){
TextView wordletters[] = {wordLetter1, wordLetter2, wordLetter3,
wordLetter4, wordLetter5, wordLetter6, wordLetter7};
TextView currentPos = null;
for(int i=0; i<wordletters.length; i++){
if(!wordletters[i].toString().equals(("*")) && wordletters[i].equals("")){
currentPos = wordletters[i];
return currentPos;
}
}
return null;
}
which gives error NulPointerException. Obviously I'm doing something wrong, which I don't get it.
Is my code completely wrong or just something missing?
in second way you will need to call getText method for comparing text of TextView's becuase currently you are trying to compare TextView instances from Array as:
for(int i=0; i<wordletters.length; i++){
if(!wordletters[i].getText().toString().equals(("*")) &&
wordletters[i].getText().toString().equals("")){
currentPos = wordletters[i];
return currentPos;
}
}

Categories

Resources