How to count down days in listview or compare given date with system date

367 views
Skip to first unread message

Ray Heer

unread,
Mar 23, 2016, 7:15:25 AM3/23/16
to MIT App Inventor Forum
Hi all,

I have tried so many solutions on this issue and none worked. I want to give in a name of a book borrowed to a student for say 14 days. I then set the date when the book has to be returned. 
The problem is that I tried several countdown methods which I found as given solutions but they don't seem to work in the list which I saved to tinydb. 
Has any one have a proper solution for me. It is supposed to be so simple but I don't seem to manage it. 

I add the book title and set the date it has to be returned and save it to the list which is then saved to the tinydb.

Thanks

Ray

Taifun

unread,
Mar 23, 2016, 10:08:01 AM3/23/16
to MIT App Inventor Forum
probably the easier task is to compare a given date with system date
see the documentation of the clock component for methods you can use

It would really help if you provided a screenshot of your relevant blocks, so we can see what you are trying to do, and where the problem may be.

Taifun

Trying to push the limits of App Inventor! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun. 

Taifun

unread,
Mar 23, 2016, 10:51:05 AM3/23/16
to MIT App Inventor Forum
see also this example http://stackoverflow.com/a/36115708/1545993
Taifun

Ray Heer

unread,
Mar 24, 2016, 7:28:18 AM3/24/16
to MIT App Inventor Forum
Hi Taifun,

The link you posted explains all the clock components which I have read several times. The example I got just leaves a blank screen. Attached the link and my result

http://stackoverflow.com/a/36115708/1545993


Is it possible to use the datepicker to set the date? It would be unpractical to generate the apk every time a new date would be set.


Ray

TaifunTest.jpg

Ghica

unread,
Mar 24, 2016, 12:13:54 PM3/24/16
to MIT App Inventor Forum
If you tried this yesterday, the visibility of the label would be set to false. Therefore you would not see anything. Today, since it is march 24th, past midnight, the result should be > 0 and you should see whatever you have in label1.
From your example it is totally unclear what you are trying to do. Maybe it is a good idea to post the blocks that you use to store the list and the way you retrieve the data you need for the comparison.
Cheers, Ghica.

Abraham Getzler

unread,
Mar 24, 2016, 3:26:27 PM3/24/16
to MIT App Inventor Forum
From the original post, where you describe keeping book titles and due dates,
I'm guessing you want to filter the stored list of books and due dates to just show
the book titles and due dates of those that have come due today?

See this sample app in the gallery and its doc for how to filter a list of lists.

The attached WHERE_EQ_NUMBER function can be copied and turned into a WHERE_AFTER_DATE filter function
using the comparison techniques in the prior posts in this thread.

ABG




WHERE_EQ_NUMBER.jpg

Ray Heer

unread,
Mar 25, 2016, 4:56:37 AM3/25/16
to MIT App Inventor Forum
Hi Abraham,

Thanks for the tip. This would also be a possibility of setting up the app. 

I want to make a list with books which are borrowed to pupils. I want to display the title, the person who borrowed it and the date it has to be returned. The problem I have is to have the date count down. I would like to know how I can make an instant of the dates in the list which is saved to tinydb. I have included a screenshot. Here I would like to know how I can make an instant from the date which I pick with the datepicker. I am using the countdown example of ByeByeAI. Hope you can assist me here.

Ray
Abrahab1.jpg

Taifun

unread,
Mar 25, 2016, 10:11:06 AM3/25/16
to MIT App Inventor Forum
instead of storing instants in TinyDB you should store millis...
Taifun

Ray Heer

unread,
Mar 25, 2016, 10:30:30 AM3/25/16
to MIT App Inventor Forum
Thanks for the tip Taifun, will give it a try and get back to you.

Abraham Getzler

unread,
Mar 25, 2016, 11:06:39 AM3/25/16
to MIT App Inventor Forum
Do as @Taifun said, using milliseconds.
Be aware that when you convert an Instant into milliseconds,
you are really converting into Milliseconds from the start of 1970,
a big number, so don't be surprised by that.
ABG

Ghica

unread,
Mar 25, 2016, 1:33:51 PM3/25/16
to MIT App Inventor Forum
Actually, I would suggest to store the date as a text string in the format mm/dd/yyyy. In this way you can read what you have in your TinyDB and make clock instances from that text only when needed.
Here is a little example app, that can add names, book titles and return dates as a list of lists to a TinyDB. 
When you display the list of books, a check is made whether they are late and then LATE is added to the item.
This is just an example, where you could add a possibility to edit items, or to delete them, and to display the list in a nicer way using an HTML table.
Cheers, Ghica.



liblist.aia

Ray Heer

unread,
Mar 26, 2016, 6:53:48 AM3/26/16
to MIT App Inventor Forum
Thanks so much Ghica, this is what I was looking for. Thanks for this great Easter gift. Could you let me know how I can change the LATE test to red when that status occurs and also how I can delete entries when books are returned?

Thanks and Greetings

Ray

Ghica

unread,
Mar 26, 2016, 5:05:00 PM3/26/16
to MIT App Inventor Forum
Hi Ray,
It is not possible to format individual lines of a multi-line label in App Inventor, therefore you cannot make the late entries red. With some work it would be possible to create a HTML table which you could view in a webviewer, there are example of using HTML, JQuery etc. on Taifun's site (http://puravidaapps.com/tutorials.php), however I advise you strongly to learn some more about App Inventor first. Start here: http://appinventor.mit.edu/explore/ai2/tutorials.html

To be able to delete one of the books from the list, you could use a listPicker to select it, remember the index and then delete it from the list when you press the delete button. Actually, as an extra easter present, I did this for you, because it requires some advanced list juggling. In the same way as I did the Delete button, you could do an Edit button too, for example to change the return date or whatever.
Cheers, Ghica.
liblist_LP.aia

Ray Heer

unread,
Mar 27, 2016, 6:04:52 AM3/27/16
to MIT App Inventor Forum
Hi Ghica,

Thanks so much for all your efforts, it is really much appreciated. I will try and figure out the editing part now.

Cheers and HAPPY EASTER

Ray

Ghica

unread,
Mar 28, 2016, 4:42:15 AM3/28/16
to MIT App Inventor Forum
Ray, PLEASE do not reply to me privately.
You wrote:
Do I have to just change the list block when making an edit button and just replace the listblock with replace item. The same is with the search button I made. Both functions return a blank screen. Can it be because I made the option to open a new screen for these two functions?
>>

I suspect that you get an empty screen because the values of global variables are not passed between screens. There is only one TinyDB, so you could use what is in there. But I would suggest that you do not try to solve two problems at the same time, that is unwise coding practice. First solve the editing and searching, then read up on managing multiple screens. There are plenty of forum appends that point to plenty of documentation.

The edit button is almost a combination of the delete and add buttons. First you pick a book, then you show the values in the edit fields (the code for that is already there in the after picking block). To make it nice you could enable and disable the appropriate blocks at the appropriate times. When the edit button (a better name would be save) is clicked, you make a list as in the add button, you replace this list and the list of books, by using replace instead of remove as in the delete button code, you store the list in the TinyDB and to show the updated list, you read it back again, as for the delete button.
Cheers, Ghica.

Ray Heer

unread,
Mar 28, 2016, 8:02:27 AM3/28/16
to MIT App Inventor Forum
Hi Ghica,

Thanks for the tips. I have read a few samples of how to do the edit part. I have put the blocks as described in some tutorials but get this error message as seen in the screenshot added. Can you tell me what this error means and how I should correct it.

Cheers

Ray
Ghica.jpg

Ghica

unread,
Mar 28, 2016, 8:48:32 AM3/28/16
to MIT App Inventor Forum
Ray,
I have no idea what the error message means, maybe the if does not like to get a string instead of true or false. What were you thinking that would happen with te result of calling that procedure??
Remember also, that the list you have in the TinyDb and therefore the global bibList is a list of lists, you should NOT format the item. otherwise you do not know anymore what is the name or the book title etc. when you read it back.

I told you to make a copy of whatever was the code for the delete button, and then replace the delete item by an apprapriate replace. If you do that, you will get this:

The if here is to make sure that you actually picked a book item first, otherwise it is not clear what you are editing or deleting. At the end of the when btnEdit.Click (or the when btnDelete.click) the pickSelectedIndex is set to 0, to make sure that you do not accidentally overwrite or delete a wrong book. The pickSelectedIndex is set in when ListPicker1.AfterPicking, to the ListPicker1.SelectionIndex.


To make the editing better, you could add the following in the Afterpicking block:


What this does, is to show the retrun date as set in the list instead of today's date. You could also add enabling the Edit and Delete buttons here and disable them whenever you set the pickSelectedIndex to 0 (in fact you would not need the if anymore then, figure out why!).
Cheers, Ghica.


Ray Heer

unread,
Mar 28, 2016, 9:57:15 AM3/28/16
to MIT App Inventor Forum
Thanks Ghica. I did the edtButton part which does not do anything when I click on it. The afterpicking part I don't have listed as you are displaying in the second pic. I have understood your clear explanations but seem to be stuck here, sorry for bothering.

Ray

Ghica

unread,
Mar 28, 2016, 10:38:11 AM3/28/16
to MIT App Inventor Forum
Hi Ray,
The edit button only does something when you picked a book first using the listPicker.
The other block I showed is not in your version, I added it to allow the editing to work better. As said, when you edit a book item, you would like that the datePicker opens on the date you had in your list before and not the today date. That is what this does.
I attach my latest version, which also implements disabling the buttons when appropriate.
Cheers, Ghica.
liblist_LP2.aia

Ray Heer

unread,
Mar 28, 2016, 11:23:13 AM3/28/16
to MIT App Inventor Forum
Ghica, sorry, not working with my version, I get the following error when starting the emulator. Can it be that you have a newer version than I have of the MIT?
Gica2.jpg

Ray Heer

unread,
Mar 28, 2016, 11:37:58 AM3/28/16
to MIT App Inventor Forum
and this is what happens when I click on the edit button
Ghica3.jpg

Ghica

unread,
Mar 28, 2016, 1:42:41 PM3/28/16
to MIT App Inventor Forum
I never use the emulator, it is a pain. If you have a real phone it is so much easier to use. I may have a newer UI companion, but the hide keyboard was also in the previous one.

Anyway, it is no essential. Just disable it (click with your right mouse button on it and choose disable) and try again.
Cheers, Ghica.

Ghica

unread,
Mar 28, 2016, 5:07:48 PM3/28/16
to MIT App Inventor Forum
I saw that there is indeed a problem with the hideKeyboard. The app sometimes complains. You can just delete the block. Possibly there are better places to put it and it is annoying if the keyboard stays visible.
You could add the hideKeyboard in the Click event blocks, but for now I hope you get it working first.
Cheers, Ghica.

Ghica

unread,
Mar 28, 2016, 6:11:38 PM3/28/16
to MIT App Inventor Forum
To my regret, there seems to be a subtle bug with hide keyboard that I am going to report.
Also there were some other problems. If you tried to add a book, without ever setting the return date, that would result in an error. Instead of some tedious checking, I added a label to contain the return data instead of the text on the date picker.
Another problem was, that if your book had a return date of today, it would show up as LATE. That could be corrected by adding a day to the comparison.
Here is a new version, without hiding the keyboard and those other  problems corrected. I hope for the best!
Cheers, Ghica.
liblist_LP3.aia

Ray Heer

unread,
Mar 29, 2016, 8:51:34 AM3/29/16
to MIT App Inventor Forum

Hi Ghica,

 

Thanks for the feedback. I thought that there had to be something not right as I had some problems testing it on the phone. There is the call TinyDB1 floating on its own, where must this be docked on to? Screenshot attached.

 

I want to try and make the search book function now and hope it will go easier. Can I arrange for the books returned at first to show on top of the list?

 

Greetings

 

Ray

Ghica4.jpg

Abraham Getzler

unread,
Mar 29, 2016, 10:29:04 AM3/29/16
to MIT App Inventor Forum

Ray Heer

unread,
Mar 29, 2016, 2:06:10 PM3/29/16
to MIT App Inventor Forum
Thanks for the tip Abraham, appreciate it. Cheers Ray

Ray Heer

unread,
Mar 29, 2016, 2:09:16 PM3/29/16
to MIT App Inventor Forum
Hi Ghica,

I made a scanner for the ISBNs. I get the following error as attached, also the blocks used are attached. What is causing this error, did it according to other tutorials.

Cheers

Ray
Ghica5.jpg
Ghica6.jpg

Ghica

unread,
Mar 29, 2016, 3:00:49 PM3/29/16
to MIT App Inventor Forum
The loose block is there for debugging. Right click on it, choose DoIt, and your book list in the TinyDB will be empty. Handy, if you made a mess off it when testing.

What are you trying to do with the result of the barcode scanner??
After you did the scan, presumably in Screen2, you set the result of the scan in some textField.
Then you display the list of books. Why? Did you display the list before on Screen2?
Next, you are opening Screen1. Two problems here:
1. Did you close Screen1 when you opened Screen2? If not, you will have problems, and you should close Screen2 instead, to return to Screen1. If yes, be aware that nothing in Screen1 will still be there, it is as if you restarted the app, although you can do something with the value passed. You also loose the result of your scan, because, as foar as I can see, you only showed it in this textField, but you did not store in in the TinyDB.
2. The startvalue in opening Screen1 is a strange thing. FormatItem is supposed to format 1 book (which, in my example is a list of three things: name, title and return date). But your argument is the whole book list. Is there anything in it?? If not, that would explain your error. If yes, you will probably get other unpredictable results.

I do not know what the other tutorials looked like, but certainly it does not fit with what you have and you should adapt it.
Is the scanner supposed to provide all information about the book? Did you want to add the information, together with the name of the borrower and the the return date to the list of books? In that case you need to expand what you did for editing or adding books. 
Cheers, Ghica.

Ray Heer

unread,
Mar 30, 2016, 5:44:20 AM3/30/16
to MIT App Inventor Forum
Thanks for the the directions and tips. For now I only want the ISBN code to be added to the Name, Item and return date. It is more convenient scanning the title that typing in those long codes every time. I am going to try doing it like you have explained, thanks again.

Greetings

Ray

Ghica

unread,
Mar 30, 2016, 8:29:03 AM3/30/16
to MIT App Inventor Forum
Hi Ray,
I think I explained how not to do it. Maybe here some tips on how to do it.

The thing you have in the TinyDB is a list of list, you can see this as a table (or spreadsheet), where the columns are the book title, borrower name etc, and each row represents a book, which is itself also a list of the column fields.
The global biblist is a current copy of what is in the TinyDB, because it is easier to manipulate than the TinyDB directly. That is why you have to retrieve it anytime you add or edit a book and store it back when done adding or editing. 

If you are using multiple screens (and so far I cannot see a reason why you would want to do that) then the TinyDB is still there at any other screen, but you must reload the global biblist whenever needed. 

Also, if you would add or pick a book on one screen and edit something for that book in another screen, you MUST pass the index (which is like a row number) from one screen to the next, otherwise there is no way you can know which book is involved.

Assuming you want to store the ISBN number, you must add another item to the list that represents a book (see it as adding a column to the table). To avoid updating all index numbers used the formatitem and maybe other stuff, it is easiest to add the ISBN number as the last  (4th) element in the list that represents a book. And of course you must add it in the AddBook and EditBook event blocks.
I hope to have not confused you further, happy inventing.
Cheers, Ghica.

Ray Heer

unread,
Apr 4, 2016, 5:44:30 AM4/4/16
to MIT App Inventor Forum
Hi Ghica,

I was absent for a few days. I don't seem to get further with the scan part yet but will try more. I want to do a search part now and want to ask if I must search the global bibList, bookitem or in fitem when searching a title?

Cheers Ray

Ghica

unread,
Apr 5, 2016, 5:55:53 AM4/5/16
to MIT App Inventor Forum
Ray, 
Maybe it is time to look at:
Taifuns top 5 tips to learn app inventor: http://puravidaapps.com/learn.php

Anyway, what do you want to search for, and how dop you want to represent the search result?
If you want to search for a word in the title of all books,
then you should retrieve the list of books from the TinyDB, (yes, in the bibList),
Iterate through it using a for each block, then for each element (which is a list again), take the second item, which is the title, the search the title for your word using the contains text block, and if true, do something with the result.
Cheers, Ghica.



Reply all
Reply to author
Forward
0 new messages