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.
3 Answers 3
I think is a Inheritance funda, So make your classes hierarchy as like (use of inheritance), So you have to make only one class which has all the derived propertied of its parent class, and make a ArrayList of that class only, So you don't have to make nested ArrayList. (If I am not wrong or if then please explain). :-)
Or generally make only one class which has all the properties, As you described above and use some getter,setter methods for that and using that class make your ArrayList.
4 Comments
create a custom class with getters and setters then create an arraylist as that datatype (your custom class)
List<customClass> foo = ArrayList<customClass>
same as the comment above but more info
1 Comment
All of the constructors should create (not just declare) the ArrayLists. Else they will be null. You can start them at a small size to save space. E.g
songs = new ArrayList(3);
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.