1

I am currently working on an app that will use many objects as information holders (music things - artist, song, id of album cover img, and another id of 2nd img).

I decided that it would be the best to create "Track" class and use it to make objects and store them in ArrayList.

I created the class, I created the list, but I'm having trouble with accessing it (I want to change the ImageViews and TextViews basing on current Track object).

Here's the Track Class: (Track.java separate)

public class Track {
private String mNameArtist;
private String mNameTrack;
private int mTabResource;
private int mCoverResource;

public Track(String nameArtist, String nameTrack, int tabResourceId, int coverResourceId){
    mNameArtist = nameArtist;
    mNameTrack = nameTrack;
    mTabResource = tabResourceId;
    mCoverResource = coverResourceId;
}
public String getArtistName() {
    return mNameArtist;
}
public String getTrackName() {
    return mNameTrack;
}
public int getTabResourceId() {
    return mTabResource;
}
public int getCoverResourceID() {
    return mCoverResource;
}}

And here's ArrayList declaration: (PlayActivity.java, inside onCreate method)

    ArrayList<Track> Tracks = new ArrayList<Track>();
    Tracks.add(new Track("Artist Name", "Track Name", R.drawable.tabtemplate, R.drawable.testcover));
    Tracks.add(new Track("Pink Floyd", "Comfortably Numb Solo 1", R.drawable.CNS1Tab, R.drawable.pink_floyd_the_wall));

There are more positions, but you get the idea.

Everything seems to work fine up to this point.

When I want to access it inside another method (even in the same PlayActivity.java) nothing happens or I see errors. I tried many different approaches but every single one fails. For example:

    Track.getTabResource();  // can't even use the method.
    Tracks.get(3);           // does not work as well.

I just can not use objects or that arraylist inside my methods. The "Tracks array" won't even show up in Android Studio when typing. Track does, but I can't access positions from Array.

So to sum up, is there any other way I can use my Objects (ArrayList) items inside other classes and methods?

Thank you for your help in advance.

1
  • 1
    Tracks is a local variable.. Use a field instead (like mNameArtist in Track) Commented Jun 25, 2017 at 18:05

1 Answer 1

0

Create List as instance variable and access through Object of that class.

public class PlayActivity {
        List<Track> tracks = new ArrayList<Track>();

        public void onCreate() {
            tracks.add(new Track("Artist Name", "Track Name", R.drawable.tabtemplate, R.drawable.testcover));
            tracks.add(
                    new Track("Pink Floyd", "Comfortably Numb Solo 1", R.drawable.CNS1Tab, R.drawable.pink_floyd_the_wall));
        }

        public List<Track> getAllTracks() {
            return Collections.unmodifiableList(tracks);
        }

        public Track getTrack(int index) {
            return tracks.get(index);

        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Well, thank you mate! It helped a lot. Overally my code and everything was fine, I just made superstupid mistake and declared my ArrayList in the onCreate method, while it should be outside, alongside with the rest of the variables. I did not change it from ArrayList to the normal List thought, I'm not sure if it was necessary? Anyway it works perfect now, thank you for help. I'm stupid I did not notice it myself :D. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.