• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing Arraylist values into Edit Text Fields

 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

just wondering what am i doing wrong here ? i am trying to Create a Random Number from 1-45 and add that to the Array and from there output it to my Edit Text Fields.




 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you think you're doing something wrong? What happens if you run this code, and how is that different from what you were expecting?
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for starters


Its Showing me cannot resolve symbol "i"

i did try

The App will Crash on Start up logCat is showing nothing
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first snippet won't compile because it uses a variable that is not defined - i.

The second has two problems - it runs as long as "i<=rand.size()", which is one iteration too long ("i<rand.size()" would be the correct clause), but even then it doesn't achieve the objective since it sets the same value in all 6 fields (which I'm guessing is not what you want).

I advise to use the first code snippet with "i" replaced by 0 to 5, respectively. Since the names of the 6 fields are hardcoded, you can't easily treat them in a loop anyway.
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

so you are saying something like this


would Random Math do better?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't change anything about how the math is done, it merely changes how the results in "rand" are retrieved.
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So for me to be able to Generate a Random Number from 0 - 45 six times and store that into my ArrayList
And then pass it on from its java.class to my MyActivity Class.

im declearing my RandomMath Page Class by
RandomMath page = new RandomMath();

i am also declearing my Arraylist. and the Method getList()
ArrayList<Integer> rand = page.getList();
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if that's meant as a question (and if it is, what the question might be). Does it work? At a cursory glance it seems that it should.
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:I'm not sure if that's meant as a question (and if it is, what the question might be). Does it work? At a cursory glance it seems that it should.


ok it could be that i was getting a
Null Pointer Exception on Randommath.class
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh right, there are a couple of problems in that class. You're not creating an object in line 7 for "myList" to point to. That's why line 15 would throw an NPE.

And in line 14/15 you're clobbering the value of the loop variable "i". Something like "myList.add(r.nextInt(45))" would fix that.
 
david foley
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


so now trying to get that pointed to my EditText Fields
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there's now a problem in line 13 - the loop will run indefinitely, because "myList.size()" is dynamically evaluated, and will constantly increase. Something like "i < 6" would add exactly 6 elements to the list.

You probably assumed that the "6" in line 7 is the size of the list, but it's not, it's the initial capacity (which is not related to the size of the list).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic