I'm new to Java, coming from a C/C++ background. I'm trying to write a music player app for Android and I am working on a library scanning function. I want to have a hierarchical database of the format Artist -> Album -> Song where Artist has a name and a group of Albums, an Album has a name, year, and group of Songs, and a Song has a title, track number, and file location.
I created three classes to store this information:
public class libraryElementArtist
{
public String name;
public ArrayList<libraryElementAlbum> albums;
}
public class libraryElementAlbum
{
public String name;
public String year;
public ArrayList<libraryElementSong> songs;
}
public class libraryElementSong
{
public String name;
public int num;
public String filename;
}
The idea to fill them is simple - scan through each file and add its artist first, then album, then song. Each time it checks to make sure the artist/album does not already exist before creating a new one.
Essentially, I start off creating an ArrayList to store the artist information like this:
ArrayList<libraryElementArtist> libraryData = new ArrayList<libraryElementArtist>();
Then, to add an artist to the database:
libraryElementArtist newEntry = new libraryElementArtist();
newEntry.name = song_artist;
libraryData.add(newEntry);
And then to add an album:
libraryElementAlbum newEntry = new libraryElementAlbum();
newEntry.name = song_album;
libraryData.get(artistIndex).albums.add(newEntry);
where artistIndex is the index of the album's artist in the top-level artist array.
When I run this on the device and step through it in the debugger, the libraryElementArtist items are inserted into the libraryData array and their names are correctly filled in. However, the albums field is listed as null and trying to add albums does not fill in any data.
Sorry if this is a noob question, like I said I'm new to Java and I've searched and can't find what I'm looking for. Also, this is the way I'd do such a task in C++, not sure if it's the correct Java way.
ArrayList<Songs>newEntry.albums = new ArrayList<libraryElementAlbum>();This made an array of Object[0] element show up under albums but this element does not populate still.