I'm trying to make a dynamic Custom ListView, where a user can enter a name and age, an unknown amount of times. credit to @Razgriz he helped me get the Custom ListView working. I am now trying to make it dynamic. My issue is when I instantiate the NameAndAgeClass object thru the constructor, my arraylist will show what i entered thru the onclick, but it is also showing the original instantiation a bunch of times as well, in the NameAndAgeClass class i tried to create 2 arraylists for the name and age, but i was getting a out of memory error. In the for loop in M class to add a entry to the ArrayList nameAndAgeList how would i get the size of NameAndAgeClass object right now i am using while i < 10.
public class MainActivity extends Activity {
M gg = new M();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aa);
}
public void ss(View v){
Intent intent2 = new Intent(MainActivity.this,M.class);
startActivity(intent2);
}
public void sa(View v){
gg.addit("phil");
}
}
public class M extends Activity {
static ArrayList<NameAndAgeClass> nameAndAgeList = new
ArrayList<NameAndAgeClass>();
static NameAndAgeClass nandc = new NameAndAgeClass("bill", 88);
static int ihg = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView nameAndAgeListView = (ListView) findViewById(R.id.listView);
//create your listView with your custom object
/*
get no error with this just says not loading do i want to cancel
for(int i = 1 ; i < nameAndAgeList.size() ; i ++){
NameAndAgeClass entry = new NameAndAgeClass("lou",23);
nameAndAgeList.add(entry);
}
*/
for(int i = 1 ; i < 10 ; i ++){
NameAndAgeClass entry = new NameAndAgeClass("lou",23);
nameAndAgeList.add(entry);
}
//create your adapter, use the nameAndAgeList ArrayList
CustomListViewAdapterNameAndAge nameAndAgeAdapter = new
CustomListViewAdapterNameAndAge(this, nameAndAgeList);
//get your listView and use your adapter
nameAndAgeListView.setAdapter(nameAndAgeAdapter);
nameAndAgeListView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int
i, long l) {
/*
Do what ever you want inside this onItemClick function
*/
}
});
}
public void addit(String nn){
ihg++;
nameAndAgeList.add(( new NameAndAgeClass("phill",ihg)));
}
}
public class NameAndAgeClass {
static public ArrayList<String> namee = new ArrayList<String>();
static public ArrayList<Integer> agee = new ArrayList<Integer>();
String name;
int age;
public NameAndAgeClass(String name, int age) {
this.name = name;
this.age = age;
namee.add(name);
agee.add(age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
See Question&Answers more detail:os