Displaying Certain Parameters Of The Objects Of An Arraylist Into A List View In Android
I have this ArrayList that is displayed in a listview. Right now it works but it shows some kind of array id looking names thats why I wanted it to display the names of each object
Solution 1:
Well, the problem is here:
public String[] getNames (){
int c=0;
int size = list.size()-1; <-- size problem
String[]names= new String[size]; <-- well @@while(size >=c){//names.add(list.get(c).getName());
names[c]= list.get(c).getName(); <-- ArrayIndexOutOfBoundException, I guess
c++;
}c=0;
returnnames;
}
You see it, don't you?
Here the fix:
public String[] getNames (){
int c=0;
int size = list.size();
String[]names= new String[size];
while(size >c){//names.add(list.get(c).getName());
names[c]= list.get(c).getName();
c++;
}c=0;
returnnames;
}
Solution 2:
The standard ArrayAdapter will automatically use the toString method of the object you have provided in the ArrayList. This means that it is using the default implementation of the toString method in Java which is to display a bunch of nonsense about the object. The simplest way to get it to display the name of the product would be to override the toString method inside of the product object to return only the name of the object.
Ex:
publicclassProduct{
//Some of your current code for product object@OverridepublicStringtoString(){
return name;
}
}
Solution 3:
Try doing:
setListAdapter(newArrayAdapter<String>(this, R.layout.list_item, pl.getNames()));
Instead of pl.getList().getNames().
Post a Comment for "Displaying Certain Parameters Of The Objects Of An Arraylist Into A List View In Android"