2014년 12월 10일 수요일

Quiz upload and link .txt

I'm building a quiz with checkboxes and I have 4 files .txt with more than 1000 questions and answers1, answers2 and answers 3. I guess I could use the component File to make the program read those files and display them as if they were in a list (I attach screenshot displaying questions and answers when I had few questions and answers and they were in a list).
I've tried //myfile.txt and \n to skip a line everytime but nothing appears, I'm completely lost.
I would be very grateful with any help.




see the documentation http://ai2.appinventor.mit.edu/reference/components/storage.html#File

ReadFrom(text fileName)
Reads text from a file in storage. Prefix the filename with / to read from a specific file on the SD card. for instance /myFile.txt will read the file /sdcard/myFile.txt. To read assets packaged with an application (also works for the Companion) start the filename with // (two slashes). If a filename does not start with a slash, it will be read from the applications private storage (for packaged apps) and from /sdcard/AppInventor/data for the Companion.

To convert the file into a list, just use the split at block and split at\n (new line)



Thanks Taifun for your answer but... I attach 2 screenshots with the changes done.
I don't know how to insert ReadFrom with my text files since this is a procedure and I'd like to put:
           SetQuestiosn.Text to --> ReadFrom
                                                filename --> //myfile.txt
           SetAnswers1.Text to --> ReadFrom
                                               filename --> //myfile2.txt
           ...
Instead I've done what's in the screenshots and the result is in screenshot1, nothing to see with my text files.





I don't know how to insert ReadFrom with my text files since this is a procedure and I'd like to put:
           SetQuestiosn.Text to --> ReadFrom
                                                filename --> //myfile.txt
           SetAnswers1.Text to --> ReadFrom
                                               filename --> //myfile2.txt

it does not work like this, just import the questions and convert them into a list like this

in Screen.Initialize event: 
File.ReadFrom //myfile.txt

in File.GotText event
set global Questions to split at get text
                                             \n

you can do the same for the answers
this also might help 

A Multiple Choice Quiz: How to work with the advanced features



first do the tutorials http://appinventor.mit.edu/explore/ai2/tutorials.html to learn the basics of App Inventorthen try something and follow the



Read your points and all the information (again) and did the changes (screenshot 1) but I have 2 errors: 
- First one, in screenshot 2.
- Second, ... can not accept the arguments (and here's the list with all the questions in Questions.txt (at least, they appear!).
I've been working on this for months and I think it's close, just need some more help, please.





you are trying to do 2 things the same time: 1. to read all questions and 2. to get the next question
probably it's better to step back one step and to learn first How to work with Lists by Saj?

on start of the app, first read all the questions and store them in a global variable, e.g. listQuestions
then later if you want to get the next question, just do the following

set CurrentQuestion to get CurrentQuestion + 1
set Question.Text to select list item listQuestions
                                                      get CurrentQuestion

same for the answers



Thanks for both answers!
This is where I am now. Read all the information and changed some things but again won't work.
I changed the value of the variables to text, but something is wrong, maybe there or at WhenQuestions.GotText, WhenAnswers1.GotText...
Hate to be annoying... Sorry!




as already said, to convert the questions into a list, you have to use the split at block

in File.GotText event
set global Questions to split at get text
                                              \n

same for the answers
see also the errors in your screenshot



Thanks Taifun for your help.
Now I think the blocks are ok (screenshot 1). The questions appear on my emulator but not the answers. Instead there's a mistake depending on the value given to the variables that may contain the list of questions and answers (screenshot 2).





this looks like a timing issue: you read the question and answers and at the same time you call the set_question procedure before the answers have been read completely
my suggestion is to call the set _question procedure in the Answers3.GotText event instead
also let me recommend do initailize the variables using the create empty list block




Did the changes you suggested (screenshot1) but:
- When screen initializes there's no text anywhere (neither questions nor answers).
- I've put a clock in 15 seconds to answer and then a 'Next' button appears and at this point the text in label 'Question' appears, but the checkboxes still are with the name I gave them in the designer, so no changes yet.
I've put the 'set_question' procedure under every WhenQuestions.GotText, WhenAnswers1.GotText... and nothing changes.
Moreover, it returns the same error though I've initialized the variables using the create empty list block, as you suggested (screenshot2).
Thanks for your patience.





you should try to understand that timing issue: to read 4 large files and convert them into lists may take a few seconds
let's assume, the questions are read and assigned to the Questions variable, but the answers are still not read: now you are trying to read the corresponding answers and get that error...

to be on the safe side only after all 4 files have been read and assigned to their variables, then call the set_question procedure



hank you very very much, Taifun!
I was making a mistake but now it works! I have another issue but I'll try some things before asking.
Only one question: My app is in Spanish and it's full of accents (aún, próximo...) and it returns another symbol in the app. Is it possible to solve this or the only solution is to eliminate them in my text files?



make sure to store the text files in UTF8 format
How to get european or chinese characters from Web ResponseContent
see also here



I'll have a look! Many thanks again!!!


댓글 없음:

댓글 쓰기