For help with installation, bugs reports or feature requests, please head over to our new forums.
Genuitec Community on GitHub
- This topic has 21 replies, 3 voices, and was last updated 12 years, 1 month ago by
Code_A.
-
AuthorPosts
-
NikolayMantchevMemberNote: You have to realize that I am programming for a high school tech fair with no formal lessons in programming. I’m just trying my hand at mobione, so please, bear with me.
I am programming a Native Android app for Samsung 10″ Tablets for this tech fair. The ideal of the app is a triarchic intelligence test, consistent of 21 questions sorted into 3 groups of 7. The idea is that the user will be able to select which statements apply to them from a select list. Then, a (fairly long) JavaScript snippet will add up how many questions of each group were selected, and, depending on which group came out on top, navigate to a different results page.
My question is about the selection process. I want to find out whether or not each item in the select-list is selected or not. If it is, I add “1” to the corresponding variable. If not, I move on. I have searched far and wide and have not found a way to check if a list item is selected or not. In addition, I am unsure if there are any special rules for Mobione. Do I have to add #m1- to every ID that I reference in the JavaScript? Is that only for page names? I’m a bit confused about the interaction between Mobione, JQuery, and JavaScript.
Thank you for your time.
January 14, 2014 at 9:28 am #346001
BrandonMemberOff the top of my head I would use variables for the groups:
var group1;
var group2;
//etc…The check the item to see which one is selected and adjust the variable according to the items value:
For example if you are using a list item you can set the item label to your visible text and the value to a number, which will add or subtract from the variables defined above. How you want to check through them is up to you, my first thought is to to use a loop. Like if you have four groups and an answer for each group you could use:
//multiple select list: get selected items as array
$(‘select[name=”selectlist1″] :selected’).each(
function(i, selected) {
if (i == 1)//check for first group item
{
group1 += $(‘select[name=”selectlist1″]’).val();//add the items value, note, this can be a negative number to subtract
}
if (i == 2)//check for first group item
{
group2 += $(‘select[name=”selectlist1″]’).val();//add the items value, note, this can be a negative number to subtract
}//etc…
}
)Hope that helps.
For your other question:
“Do I have to add #m1- to every ID that I reference in the JavaScript? ”
yes and No, there are some (like my example above) that you do not have to, but I have found its best to use it if unsure. Maybe support can help clear this one up a little more.January 14, 2014 at 8:52 pm #346036
NikolayMantchevMember@CincyPlanet wrote:
Off the top of my head I would use variables for the groups:
var group1;
var group2;
//etc…The check the item to see which one is selected and adjust the variable according to the items value:
For example if you are using a list item you can set the item label to your visible text and the value to a number, which will add or subtract from the variables defined above. How you want to check through them is up to you, my first thought is to to use a loop. Like if you have four groups and an answer for each group you could use:
//multiple select list: get selected items as array
$(‘select[name=”selectlist1″] :selected’).each(
function(i, selected) {
if (i == 1)//check for first group item
{
group1 += $(‘select[name=”selectlist1″]’).val();//add the items value, note, this can be a negative number to subtract
}
if (i == 2)//check for first group item
{
group2 += $(‘select[name=”selectlist1″]’).val();//add the items value, note, this can be a negative number to subtract
}//etc…
}
)Hope that helps.
For your other question:
“Do I have to add #m1- to every ID that I reference in the JavaScript? ”
yes and No, there are some (like my example above) that you do not have to, but I have found its best to use it if unsure. Maybe support can help clear this one up a little more.Thanks soooo much man. But i have a question? What exactly is the “i” variable in the above function? Is it the value of the first item in the array? because you said \\Check for the first group item in front of both i==1 and i==2. I’m a little confused as to that.
Otherwise, your reply has been MOST helpful.
January 15, 2014 at 7:45 am #346047
BrandonMemberThe i will represent the current item in the array, starting with 0 being the first item in the list.
January 15, 2014 at 5:48 pm #346076
NikolayMantchevMemberThank you again, Cincy, but I have a sort of hypothetical final question.
My test is consistent of 21 questions, with the questions belonging to 3 groups. They are not in order.
Does this mean I have to do an “if” statement for all possible selected questions (so, at most, i==20, as that would mean all 21 questions were selected)? And i also have to check each question’s value (at the moment, the value of the question is what group it belongs to)? Considering there are 3 groups and max 21 selectable questions, would i have to do 63 “if” statements? is there a shortcut around this?Again, you have been a great help, thanks for all your time.
January 16, 2014 at 8:56 am #346093
BrandonMemberDo you have a sample layout you can share to help visualize it?
It really depends on what result you are going for. Another approach may be to add/subtract to the group variables when they are selected. This will give your Groups a total number.
But, if you need to know the specific answer to each question you have may to cycle through them, not as hard as it sounds using the loop though.
January 16, 2014 at 10:02 am #346094
Code_AMemberWhat about creating 3 different lists of 7 and then just loop through each one individually, add up the number if selected items from each list, and then compare the 3 totals at the end?
You should be able to design the layout so that there is no “break” between the lists and you can programmatically remove the “first” and “last” CSS classes from the list items so they do not appear rounded on the screen.
Would something like this give you the result you are looking for?
January 19, 2014 at 5:23 pm #346197
NikolayMantchevMemberCodeA: That sounds like a great idea, although I’m not aware about how to mess with the CSS files to remove the rounding. Also, my questions are not grouped up, 7 first, 7 second, 7 third, but rather i have questions from each group mixed in. would that work? And how would i get rid off the roundedness and make everything look together (or apart)?
January 19, 2014 at 5:40 pm #346198
NikolayMantchevMember@CincyPlanet wrote:
Do you have a sample layout you can share to help visualize it?
well imagine this.
var group1;
var group2;
//next we have the thing that loops
$(‘select[name=”selectlist1″] :selected’).each(
function(i, selected) {
if (i==0) { //first item in list, I’m pretty sure it starts with 0
++group1 //adding a numerical value of 1 to group1, though i’m not sure if this works in javascript
}
if (i==0) { //second item in list
++group2 //again, not sure if this works in javascript, or if it is group2++
}Now, this assumes that both answers are yes. If question one is answered only, then group 1 is added to. But what if ONLY question 2 is answered and the loop goes through. Since it is technically the first thing in the loop of selected objects because it is the only selected object, would it come out as i==0 and then end up adding a value of 1 to variable group1? Or would javascript know that, because it is question two, it is the second item in the loop? If there is only one question answered, doesn’t matter which question, wouldn’t that always be the first thing in the loop?
January 19, 2014 at 9:23 pm #346199
Code_AMember@philippnm wrote:
my questions are not grouped up, 7 first, 7 second, 7 third, but rather i have questions from each group mixed in. would that work?
No. My 3 list idea would only work if the questions were all grouped together.
Here is another idea. If you only need to know the groups selected then you could use the value property of the list item to assign the group number, then when you loop through the selected items you could just count how many of each group is selected.
I have attached an example.
Attachments:
You must be logged in to view attached files.January 19, 2014 at 9:35 pm #346200
NikolayMantchevMemberCodeA Thanks man!
January 19, 2014 at 9:59 pm #346201
Code_AMemberNo problem. I hope it helps.
January 23, 2014 at 6:07 pm #346329
NikolayMantchevMemberHey CodeA, I have a question about the thing you sent me.
if (itm){//parse selected items
var str = itm.toString(); //this needs converted to a string
var parse = str.split(“,”);//tally groups
for (i=0; i<parse.length; i++){
if (parse[i] == 1) {
group1++;
}else if(parse[i] == 2){
group2++;
}else{
group3++;
}What is the significance of Parsing here? You parse the the value of the selected list items with str.split (which i’m not very familiar with), and then you proced to use it to determine which group it goes into, and you use it to keep the loop going. What does parse do? What does for(i=0; i<parse.length; i++) do? What is that piece of code supposed to do, keep the string going until it runs out of things?
Also, I need to put a piece of code in that navigates to a new page. Would that be phoneui.gotoScreen, or a variation of it?
January 23, 2014 at 7:53 pm #346330
Code_AMemberThe first thing I did was read the value of the list. Remember, in the example I gave you I used the value property of the list item to store its group number. Since the list is set to multiple selection, the selected values get returned in csv (comma separated values) format. The split function will split (or parse) a string into an array based on a separator, a comma in this case. Here is a link to how the split function works.
Then I loop through the array (called parse in this example) based on its length (parse.length). The length of the array is determined by the number of items selected in the list. Here is a link to how the for loop works.
And yes, you should be able to just add the code phoneui.gotoScreen(‘#m1-screen’) at the end to switch screens.
Last thing, to see the string before it gets parsed just add an alert before the split function.
//parse selected items var str = itm.toString(); //this needs converted to a string alert(str); var parse = str.split(",");January 23, 2014 at 8:17 pm #346331
NikolayMantchevMemberThanks man, you’re a lifesaver. I was a just a bit confused as to what the parsing was actually doing. You’ve been a real help. Thanks!
Note: I’ve also seen phoneui.gotoPage to switch pages. Is this the same as phoneui.gotoScreen?
-
AuthorPosts
