Remove All Ads from XDA
Honor 9 Lite

[Q] Java/android not understanding oop

22 posts
Thanks Meter: 0
By madmcphil, Junior Member on 3rd May 2012, 03:08 PM
Post Reply Email Thread
Hi guys,

Im really trying to learn java but think Im getting very confused. I have a class which holds names and information, In my android program I have the user enter a load of names which then are passed as an array to my next activity. Whenever I try to create objects from each name in the array it wont do it? Ialso understand that this may be a bad way of doing this as names will change so how would I reference them. Maybe using the array[pos] to create objects... so instead of name.printweight(); I could use 1.printweight();

I really hope above is readable or understandable as to what I am trying to learn


Take in names of competitors
Pass array of competitors to next activity
for loop to get each competitor name entered to a string
create an object using the competitors name (e.g name.birthday)

class competitor:
highest score

I think you cant use a string name or an int to create objects but how else would I create several competitors in my class?

Im reading some books I bought and I really am struggling to make sense of all this. Please be patient with me...

Kind regards

3rd May 2012, 06:23 PM |#2  
OP Junior Member
Thanks Meter: 0
Think I got it.....

for (int i = 0; i < numcompetitors; i++)
String competitorName = (String) competitorsArray[i];
TextView txtOutput = (TextView) findViewById(;
txtOutput.append(competitorName + " ");
//create an object of this competitor
competitorsArray[i] = new Competitor();
((Competitor) competitorsArray[i]).average();
3rd May 2012, 10:38 PM |#3  
Senior Member
Thanks Meter: 46
I couldn't really understand what you are having a problem with. The code you posted might be working but it really doesn't seem like a good way of doing what you are trying to do.

I'd like to help you out but first can you please explain a bit more clearly? Maybe even post your code, so that its easier for me to understand.
4th May 2012, 12:00 AM |#4  
RoberGalarga's Avatar
Senior Member
Flag In front of my laptop :D
Thanks Meter: 273
So... if I understood you, you need something like this:
class competitor:
highest score

Names entered:

A.dob, A.weight, A.highestscore, A.time
B.dob, B.weight, B.highestscore, B.time
C.dob, C.weight, C.highestscore, C.time

Right? Well, it can't be achieved using Java, it doesn't allow you to assign programatically names to variables. However, you can use a little trick based on an array:
//Remember: you have a class named "competitor" containing several variables
String[] Names= {"Uno","Dos","Tres"};  //the array passed by the other activity
competitor[] playerData=new competitor[Names.length];  //an array, to store copies of the competitor class (a copy for each name entered)

for(byte i=0;i<Names.length;i++){
  playerData[i]=new competitor();  //Initializing the copy...
    playerData[i].dob=     //Storing data for each name entered...
And that's all... 1st player's data will be stored in the position 0 of the array, 2nd player's data in the position 1... and so on.

BTW, instead use 2 activities, is more easy to use only an activity, changing its layout when you need a GUI's change (yep, it's possible ).
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes