Thursday, May 9, 2013

[android help] how to use simple getter and setter with android


I have a class country with an arraylist that stores countries. I have created a get and set to add and get items from specified indexes from the array list but i wont work. Whenever i call an index from the arraylist i get an out of bounds exception because the array is empty or at least seems to be.



public class country extends Application {

public ArrayList countryList = new ArrayList();
public String Name;
public String Code;
public String ID;

public country()
{

}

public country(String name, String id, String code)
{
this.Name = name;
this.ID = id;
this.Code = code;
}

public void setCountry(country c)
{
countryList.add(c);
}

public country getCountry(int index)
{
country aCountry = countryList.get(index);
return aCountry;
}


to call the setter i use.



country ref = new country();

ref.setCountry(new country (sName, ID, Code));


then when i want to get an index



String name = ref.countryList.get(2).Name;


i have done the same thing but used a local arraylist and it populated fine and i was able to display the names so the datasource isnt the problem its whatever im doing wrong setting and getting the data inside the arraylist in the country class



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...