I am trying to log the contents of an arraylist. The arraylist contains a custom object with 4 parameters. When I execute the following code I get the following log. what i want is a string of [name,class,thirty,ninety],[name,class,thirty,ninety], etc
Custom Object Class:
public class Data {
String NAME;
String CLASS;
String THIRTY;//this is the thirty day supply
String NINETY;//this is the ninety day supply
public Data(){
super();
}
public Data(String NAME, String CLASS, String THIRTY, String NINETY){
super();
this.NAME = NAME;
this.CLASS = CLASS;
this.THIRTY = THIRTY;
this.NINETY = NINETY;
}
}
Activity:
ArrayList<Data> array = new ArrayList<Data>();
Cursor c = mDBHelper.getReadableDatabase().query(Constants.K_Table, null, Constants.Name +"=?", new String[] {SEARCH}, null, null, null);
startManagingCursor(c);
if (c.moveToFirst()){
NAME = c.getString(c.getColumnIndex(Constants.Name));
CLASS = c.getString(c.getColumnIndex(Constants.Class));
THIRTY = c.getString(c.getColumnIndex(Constants.Month));
NINETY = c.getString(c.getColumnIndex(Constants.Three_Month));
array.add(new DrugData(NAME, CLASS, THIRTY, NINETY));
Log.d(TAG, "array contains: "+ array.toArray().toString());
}while(c.moveToNext());
Log:
01-28 22:31:44.742: D/Activity(23656): the array contains: [com.Data@40552050]
To achieve your objective to print String of object you have to do two things in your code.
1) Override
toStringmethod.2)
ArrayList<Data> arrayis collection of Data objects. You have to specify which object you want to print. You can do that in two ways.You can loop through your array to print all Data objects like this