Click here to Skip to main content
15,607,804 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following project and it seems to be giving me problem.

The project runs and set everything for one day but then next day as the date changes it doesn't set the values from excel file to the selected fields.

I am not much of an expert but i would appreciate if someone could help me resolve the issue.

The load button is there to load an excel file to retrieve the data from also if the program works the refresh button doesn't need to be there so it can be removed.

The following is the whole project:

Java
  1  package com.example.timings;
  2  
  3  import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat;
  4  
  5      import android.Manifest;
  6      import android.annotation.SuppressLint;
  7      import android.content.DialogInterface;
  8      import android.content.Intent;
  9      import android.content.pm.PackageManager;
 10      import android.content.res.AssetManager;
 11      import android.os.Build;
 12      import android.os.Bundle;
 13      import android.os.Environment;
 14      import android.os.Handler;
 15      import android.text.format.DateFormat;
 16      import android.util.Log;
 17  
 18      import android.view.View;
 19      import android.view.WindowManager;
 20      import android.widget.Button;
 21      import android.widget.EditText;
 22      import android.widget.TextView;
 23      import android.widget.Toast;
 24  
 25      import java.io.File;
 26      import java.io.FileReader;
 27      import java.io.IOException;
 28      import java.io.InputStream;
 29      import java.text.ParseException;
 30      import java.text.SimpleDateFormat;
 31      import java.util.Date;
 32      import java.util.Objects;
 33  
 34      import jxl.Cell;
 35      import jxl.Sheet;
 36      import jxl.Workbook;
 37      import jxl.read.biff.BiffException;
 38  public class MainActivity extends AppCompatActivity {
 39  
 40  AlertDialog dialog;
 41  EditText editText;
 42  TextView Fajr, Zuhar, Asr, Magrib, Isha, Namaz, nextnamaz, NoticeText;
 43  String fajr24, zuhar24, asr24, magrib24, isha24, friday24, countdowndt, Notice, fridayCheck, Midnight = "";
 44  private TextView countdowntxt;
 45  final Handler handler = new Handler();
 46  private Runnable runnable;
 47  final String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss a";
 48  private boolean FridayCheck = false;
 49  Button Noticebutton, loadbutton;
 50  
 51  
 52  private boolean isExternalStorageAvailableForRW() {
 53      // Check if the external storage is available for read and write by calling
 54      // Environment.getExternalStorageState() method. If the returned state is MEDIA_MOUNTED,
 55      // then you can read and write files. So, return true in that case, otherwise, false.
 56      String extStorageState = Environment.getExternalStorageState();
 57      if (extStorageState.equals(Environment.MEDIA_MOUNTED)) {
 58          return true;
 59      }
 60      return false;
 61  }
 62  public boolean isStoragePermissionGranted() {
 63      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
 64          if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
 65                  == PackageManager.PERMISSION_GRANTED) {
 66              //Permission is granted
 67              return true;
 68          } else {
 69              //Permission is revoked
 70              ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
 71              return false;
 72          }
 73      } else {
 74          //permission is automatically granted on sdk<23 upon installation
 75          //Permission is granted
 76          return true;
 77      }
 78  }
 79  
 80  
 81  @Override
 82  protected void onCreate(Bundle savedInstanceState) {
 83      super.onCreate(savedInstanceState);
 84      setContentView(R.layout.activity_main);
 85  
 86  
 87      ActivityCompat.requestPermissions(this, new String[]{
 88              Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
 89  
 90      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
 91      getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
 92  
 93      if (!isExternalStorageAvailableForRW()) {
 94          loadbutton.setEnabled(false);
 95      } else {
 96          ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
 97  
 98      }
 99  
100  
101      NoticeText = findViewById(R.id.Noticeid);
102  
103      Namaz = findViewById(R.id.namazid);
104      nextnamaz = findViewById(R.id.next_namaz);
105      countdowntxt = findViewById(R.id.countdown);
106      Noticebutton = findViewById(R.id.Noticebtn);
107      loadbutton = findViewById(R.id.button_load);
108      dialog = new AlertDialog.Builder(this).create();
109      editText = new EditText(this);
110      dialog.setTitle("Edit The Notice");
111      dialog.setView(editText);
112  
113      Date d = new Date();
114      CharSequence dt = DateFormat.format("MMM dd, yyyy", d.getTime());
115      CharSequence dt1 = DateFormat.format("EEE", d.getTime());
116      Log.wtf("MY ACITVITY", "Current date 123: " + dt1.toString());
117  
118  
119      CharSequence currentdate = DateFormat.format("yyyy-MM-dd", d.getTime());
120      Log.wtf("My Activity", "current date : " + currentdate);
121      fajr24 = currentdate.toString() + " ";
122      zuhar24 = currentdate.toString() + " ";
123      asr24 = currentdate.toString() + " ";
124      magrib24 = currentdate.toString() + " ";
125      isha24 = currentdate.toString() + " ";
126      friday24 = currentdate.toString() + " 02:00:00 PM";
127      Midnight = currentdate.toString() + " " + "24:00:00 PM";
128      fridayCheck = dt1.toString();
129      Log.wtf("MY ACITVITY", "fridayCheck: " + fridayCheck);
130  
131  
132      if (Objects.equals(fridayCheck, "Fri")) {
133          FridayCheck = true;
134          Log.wtf("MY ACITVITY", "Current date 3232: " + dt1.toString());
135          Log.wtf("MY ACITVITY", "Worked!!! ");
136      }
137  
138      LoadFile();
139  
140  
141      try {
142          AssetManager am = getAssets();
143          InputStream is = am.open("timings.xls");
144  
145  //            InputStream is = am.open("timing.xls"); Workbook wb = Workbook.getWorkbook(is); Sheet s = wb.getSheet(0); int col = s.getColumns(); int row = s.getRows(); String test1 = ""; String fajrtxt = ""; String zuhartxt = ""; String asrtxt = ""; String magribtxt = ""; String ishatxt = ""; int count = 0; int test123 = 0; boolean loopon = true;
146  
147          if (loopon) {
148              for (int a = 0; a < row; a++) {
149                  for (int b = 0; b < col; b++) {
150  
151                      if (b == 0 && loopon) {
152  
153                          Log.wtf("My Activity", "Count = " + count);
154                          Cell c = s.getCell(b, a);
155                          test1 = test1 + c.getContents();
156                          Log.wtf("My Activity", "test1 = " + test1);
157                          Log.wtf("My Activity", "dt = " + dt);
158  
159  
160                          if (dt.equals(test1)) {
161                              Log.wtf("My Activity", "a  = " + a);
162                              Log.wtf("My Activity", "Worked  ");
163                              count = a;
164                              test123 = count;
165                              loopon = false;
166  
167                          }
168                          count++;
169                          test1 = "";
170                      }
171  
172                  }
173              }
174          }
175          if (loopon == false) {
176              Log.wtf("My Activity", "Loop Broke!");
177          }
178  
179  
180          for (int i = 0; i < row; i++) {
181              for (int c = 0; c < col; c++) {
182                  if (c == 1 && i == test123) {
183                      Cell z = s.getCell(c, i);
184                      fajrtxt = fajrtxt + z.getContents();
185                      /*Log.wtf("My Activity","i = "+i);*/
186  
187                  }
188                  if (c == 2 && i == test123) {
189                      Cell z = s.getCell(c, i);
190                      zuhartxt = zuhartxt + z.getContents();
191                  }
192                  if (c == 3 && i == test123) {
193                      Cell z = s.getCell(c, i);
194                      asrtxt = asrtxt + z.getContents();
195                  }
196                  if (c == 4 && i == test123) {
197                      Cell z = s.getCell(c, i);
198                      magribtxt = magribtxt + z.getContents();
199                  }
200                  if (c == 5 && i == test123) {
201                      Cell z = s.getCell(c, i);
202                      ishatxt = ishatxt + z.getContents();
203  
204                  }
205              }
206          }
207          dFajr(fajrtxt);
208          dZuhar(zuhartxt);
209          dAsr(asrtxt);
210          dMagrib(magribtxt);
211          dIsha(ishatxt);
212          fajr24 = fajr24 + fajrtxt + ":00 AM";
213          zuhar24 = zuhar24 + zuhartxt + ":00 PM";
214          asr24 = asr24 + asrtxt + ":00 PM";
215          magrib24 = magrib24 + magribtxt + ":00 PM";
216          isha24 = isha24 + ishatxt + ":00 PM";
217  
218  
219      } catch (IOException | BiffException | ParseException e) {
220          e.printStackTrace();
221  
222      }
223  
224      countDownStart();
225      ChangeNotice();
226  
227  }
228  
229  
230  private void LoadFile() {
231      loadbutton.setOnClickListener(new View.OnClickListener() {
232          @Override
233          public void onClick(View view) {
234  
235  
236              Intent intent = new Intent(MainActivity.this, MainActivity2.class);
237              startActivity(intent);
238  
239  
240          }
241      });
242  }
243  
244  private void ChangeNotice() {
245      Noticebutton.setOnClickListener(new View.OnClickListener() {
246          @Override
247          public void onClick(View view) {
248              AlertDialog.Builder mydialog = new AlertDialog.Builder(MainActivity.this);
249              mydialog.setTitle("Edit Notice!");
250  
251              final EditText editText = new EditText(MainActivity.this);
252              mydialog.setView(editText);
253  
254              mydialog.setPositiveButton("Save", new DialogInterface.OnClickListener() {
255                  @Override
256                  public void onClick(DialogInterface dialogInterface, int i) {
257                      Notice = editText.getText().toString();
258                      NoticeText.setText(Notice);
259                      Toast.makeText(MainActivity.this, "Notice Changed!!", Toast.LENGTH_LONG).show();
260  
261                  }
262              });
263              mydialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
264                  @Override
265                  public void onClick(DialogInterface dialogInterface, int i) {
266                      dialogInterface.cancel();
267                  }
268              });
269              mydialog.show();
270  
271          }
272      });
273  
274  
275  }
276  
277  private void countDownStart() {
278  
279      runnable = new Runnable() {
280          @SuppressLint("SetTextI18n")
281          @Override
282          public void run() {
283              try {
284                  handler.postDelayed(this, 1000);
285                  SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
286  
287                  Date Fajrdt = dateFormat.parse(fajr24);
288                  Date Zuhardt = dateFormat.parse(zuhar24);
289                  Date Fridaydt = dateFormat.parse(friday24);
290                  Log.wtf("My Activity", "event_date : " + Fridaydt);
291  
292                  Date Asrdt = dateFormat.parse(asr24);
293                  Date Magribdt = dateFormat.parse(magrib24);
294                  Date Ishadt = dateFormat.parse(isha24);
295                  Date Midnightdt = dateFormat.parse(Midnight);
296                  Date current_date = new Date();
297                  Date tomorrow = new Date(current_date.getTime() + (1000 * 60 * 60 * 24));
298  
299  
300                  Log.wtf("My Activity", "current_date : " + current_date);
301                  Log.wtf("My Activity", "tomorrow : " + tomorrow);
302                  Log.wtf("My Activity", "Fajrdt : " + Fajrdt);
303  
304  
305                  if (!current_date.after(Fajrdt)) {
306                      long diff = Fajrdt.getTime() - current_date.getTime();
307                      /*Log.wtf("My Activity","event_date : "+Fajrdt);
308                      Log.wtf("My Activity","current : "+current_date);*/
309                      long Hours = diff / (60 * 60 * 1000) % 24;
310                      long Minutes = diff / (60 * 1000) % 60;
311                      long Seconds = diff / 1000 % 60;
312                      //
313                      @SuppressLint("DefaultLocale") String Hoursst = String.format("%02d", Hours);
314                      @SuppressLint("DefaultLocale") String Minutesst = String.format("%02d", Minutes);
315                      @SuppressLint("DefaultLocale") String Secondsst = String.format("%02d", Seconds);
316  
317                      countdowndt = Hoursst + ":" + Minutesst + ":" + Secondsst;
318                      /*Log.wtf("My Activity","event_date : "+Fajrdt);
319                      Log.wtf("My Activity","countdown : "+countdowndt);*/
320                      countdowntxt.setText(countdowndt);
321                      Namaz.setText("Fajr");
322                      nextnamaz.setText("Next Namaz :");
323  
324                  } else if (!current_date.after(Zuhardt)) {
325                      if (FridayCheck == true) {
326  
327                          long diff = Fridaydt.getTime() - current_date.getTime();
328                          /*Log.wtf("My Activity","event_date : "+Fridaydt);
329                          Log.wtf("My Activity","current : "+current_date);*/
330                          long Hours = diff / (60 * 60 * 1000) % 24;
331                          long Minutes = diff / (60 * 1000) % 60;
332                          long Seconds = diff / 1000 % 60;
333                          //
334                          String Hoursst = String.format("%02d", Hours);
335                          String Minutesst = String.format("%02d", Minutes);
336                          String Secondsst = String.format("%02d", Seconds);
337  
338                          countdowndt = Hoursst + ":" + Minutesst + ":" + Secondsst;
339                          Log.wtf("My Activity", "event_date : " + Fridaydt);
340                          Log.wtf("My Activity", "countdown : " + countdowndt);
341                          Log.wtf("My Activity", "Current  : " + current_date);
342                          countdowntxt.setText(countdowndt);
343                          Namaz.setText("Friday");
344  
345                      } else {
346                          long diff = Zuhardt.getTime() - current_date.getTime();
347                          /*Log.wtf("My Activity","event_date : "+Zuhardt);
348                          Log.wtf("My Activity","current : "+current_date);*/
349                          long Hours = diff / (60 * 60 * 1000) % 24;
350                          long Minutes = diff / (60 * 1000) % 60;
351                          long Seconds = diff / 1000 % 60;
352                          //
353                          String Hoursst = String.format("%02d", Hours);
354                          String Minutesst = String.format("%02d", Minutes);
355                          String Secondsst = String.format("%02d", Seconds);
356  
357                          countdowndt = Hoursst + ":" + Minutesst + ":" + Secondsst;
358                          Log.wtf("My Activity", "event_date : " + Zuhardt);
359                          Log.wtf("My Activity", "countdown : " + countdowndt);
360                          countdowntxt.setText(countdowndt);
361                          Namaz.setText("Zuhar");
362                      }
363  
364                  } else if (!current_date.after(Asrdt)) {
365                      long diff = Asrdt.getTime() - current_date.getTime();
366                      /*Log.wtf("My Activity","event_date : "+Asrdt);
367                      Log.wtf("My Activity","current : "+current_date);*/
368                      long Hours = diff / (60 * 60 * 1000) % 24;
369                      long Minutes = diff / (60 * 1000) % 60;
370                      long Seconds = diff / 1000 % 60;
371                      //
372                      String Hoursst = String.format("%02d", Hours);
373                      String Minutesst = String.format("%02d", Minutes);
374                      String Secondsst = String.format("%02d", Seconds);
375  
376                      countdowndt = Hoursst + ":" + Minutesst + ":" + Secondsst;
377                      /*Log.wtf("My Activity","event_date : "+Asrdt);
378                      Log.wtf("My Activity","countdown : "+countdowndt);*/
379                      countdowntxt.setText(countdowndt);
380                      Namaz.setText("Asr");
381  
382                  } else if (!current_date.after(Magribdt)) {
383                      long diff = Magribdt.getTime() - current_date.getTime();
384                      /*Log.wtf("My Activity","event_date : "+Magribdt);
385                      Log.wtf("My Activity","current : "+current_date);*/
386                      long Hours = diff / (60 * 60 * 1000) % 24;
387                      long Minutes = diff / (60 * 1000) % 60;
388                      long Seconds = diff / 1000 % 60;
389                      //
390                      String Hoursst = String.format("%02d", Hours);
391                      String Minutesst = String.format("%02d", Minutes);
392                      String Secondsst = String.format("%02d", Seconds);
393  
394                      countdowndt = Hoursst + ":" + Minutesst + ":" + Secondsst;
395                      /*Log.wtf("My Activity","event_date : "+Magribdt);
396                      Log.wtf("My Activity","countdown : "+countdowndt);*/
397                      countdowntxt.setText(countdowndt);
398                      Namaz.setText("Maghrib");
399  
400                  } else if (!current_date.after(Ishadt)) {
401                      long diff = Ishadt.getTime() - current_date.getTime();
402                      /*Log.wtf("My Activity","event_date : "+Ishadt);
403                      Log.wtf("My Activity","current : "+current_date);*/
404                      long Hours = diff / (60 * 60 * 1000) % 24;
405                      long Minutes = diff / (60 * 1000) % 60;
406                      long Seconds = diff / 1000 % 60;
407                      //
408                      String Hoursst = String.format("%02d", Hours);
409                      String Minutesst = String.format("%02d", Minutes);
410                      String Secondsst = String.format("%02d", Seconds);
411  
412                      countdowndt = Hoursst + ":" + Minutesst + ":" + Secondsst;
413                      /*Log.wtf("My Activity","event_date : "+Ishadt);
414                      Log.wtf("My Activity","countdown : "+countdowndt);*/
415                      countdowntxt.setText(countdowndt);
416                      Namaz.setText("Isha");
417  
418                  } else if (!current_date.after(Midnightdt)) {
419                      Date nextfajr = new Date(Fajrdt.getTime() + (1000 * 60 * 60 * 24));
420                      long diff = nextfajr.getTime() - current_date.getTime();
421                      Log.wtf("My Activity", "event_date : " + nextfajr);
422                      Log.wtf("My Activity", "current : " + current_date);
423                      long Hours = diff / (60 * 60 * 1000) % 24;
424                      long Minutes = diff / (60 * 1000) % 60;
425                      long Seconds = diff / 1000 % 60;
426                      //
427                      String Hoursst = String.format("%02d", Hours);
428                      String Minutesst = String.format("%02d", Minutes);
429                      String Secondsst = String.format("%02d", Seconds);
430                      Log.wtf("My Activity", "event_date : " + Fajrdt);
431                      countdowndt = Hoursst + ":" + Minutesst + ":" + Secondsst;
432                      /*Log.wtf("My Activity","event_date : "+Midnight);
433                      Log.wtf("My Activity","countdown : "+countdowndt);*/
434                      countdowntxt.setText(countdowndt);
435                      Namaz.setText("Fajr");
436  
437                  } else {
438                      countdowntxt.setText("ERROR!");
439                      handler.removeCallbacks(runnable);
440                  }
441              } catch (Exception e) {
442                  e.printStackTrace();
443              }
444          }
445      };
446      handler.postDelayed(runnable, 0);
447  
448  
449  }
450  
451  
452  public void dFajr(String value) throws ParseException {
453      Fajr = findViewById(R.id.fajr);
454      Fajr.setText(value);
455  }
456  
457  public void dZuhar(String value) {
458      Zuhar = findViewById(R.id.zuhar);
459      Zuhar.setText(value);
460  }
461  
462  public void dAsr(String value) {
463      Asr = findViewById(R.id.asr);
464      Asr.setText(value);
465  }
466  
467  public void dMagrib(String value) {
468      Magrib = findViewById(R.id.magrib);
469      Magrib.setText(value);
470  }
471  
472  public void dIsha(String value) {
473      Isha = findViewById(R.id.isha);
474      Isha.setText(value);
475  }
476  
477  }


What I have tried:

Well I did my best to work it out. but i think its the repeating everyday loop that i lack to understand and seems to be not working.
Please feel free to change the code as you see fit as far as it works thats what matters to me.

Many thanks
Posted
Updated 1-Nov-22 9:51am
v2
Comments
Richard MacCutchan 1-Nov-22 15:52pm    
You have just dumped almost 500 lines of code and expect someone here to analyse it all and fix your problem. Without considerably more detail as to the exact problem and where it occurs, I doubt that anyone will be able to do that.
Touch Me Not 1-Nov-22 19:26pm    
So what do you think i should do? I haven't done this before so do apologies for my part of ignorance here... do you think i should remove the code? or just post the partial bit where it loops etc?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900