Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

[Q] Java/android not understanding oop

OP madmcphil

3rd May 2012, 03:08 PM   |  #1  
OP Junior Member
Thanks Meter: 0
 
22 posts
Join Date:Joined: Jun 2010
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

SUDO:

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:
dob
weight
highest score
time

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

Phil
Last edited by madmcphil; 3rd May 2012 at 03:27 PM.
3rd May 2012, 06:23 PM   |  #2  
OP Junior Member
Thanks Meter: 0
 
22 posts
Join Date:Joined: Jun 2010
Think I got it.....

for (int i = 0; i < numcompetitors; i++)
{
String competitorName = (String) competitorsArray[i];
TextView txtOutput = (TextView) findViewById(R.id.edttxtOutput);
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: 45
 
153 posts
Join Date:Joined: Jan 2011
Donate to Me
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: 260
 
730 posts
Join Date:Joined: Dec 2011
More
So... if I understood you, you need something like this:
class competitor:
dob
weight
highest score
time

Names entered:
A,B,C

Result:
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:
Code:
//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...
      playerData[i].weight=
        playerData[i].highestscore=
          playerData[i].time=
}
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
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes