-2

I've searched in archive about my problem, but i can't find any solution. I'm new in coding, and really don't know why my button next doesn't work (crashed when button is clicked). It's definetly because of my arraylist, but why? I thought that my array list size is 20, not 5. I know that 5 is the number of songs displayed based on category clicked, but I didn't create new array list for every category. Any help?

public class listViewOfSongs extends AppCompatActivity {

// Varaibles
public ArrayList<Songs> songs = new ArrayList<>();
String songCategory;
//Needed for getting data from MainActivity
private String data;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.songs_list);

    //Getting data from main activity
    Bundle bundle = getIntent().getExtras();
    data = bundle.getString("data");

    // Display list of songs based on category chosen
    displaySongs();

}

// Method to display list of songs
public void displaySongs() {

    // Create a list of songs
    songs = new ArrayList<>();
    if (data.equals(getString(R.string.category_electronic))) {
        // Set category
        songCategory = getString(R.string.category_electronic);
        // Add songs
        songs.add(new Songs("I Don't Cry", "A Virtual Friend", 0));
        songs.add(new Songs("Light", "Hazardous",1));
        songs.add(new Songs("First", "JekK",2));
        songs.add(new Songs("Energy", "Pokki DJ",3));
        songs.add(new Songs("Pangea", "Professor Kliq",4));
    } else if (data.equals(getString(R.string.category_hiphop))) {
        // Set category
        songCategory = getString(R.string.category_hiphop);
        // Add songs
        songs.add(new Songs("Drop", "Z-Major",5));
        songs.add(new Songs("You Got Me Rocking", "Ed Napioli",6));
        songs.add(new Songs("Summertimewav", "Emcee Lynx",7));
        songs.add(new Songs("Let It Go", "Kidd Young ft. Michael John",8));
        songs.add(new Songs("Deep Club Music", "Sam Valley",9));
    } else if (data.equals(getString(R.string.category_house))) {
        // Set category
        songCategory = getString(R.string.category_house);
        // Add songs
        songs.add(new Songs("Disc Over", "Becaysi",10));
        songs.add(new Songs("Light", "Hazardous",11));
        songs.add(new Songs("Seasons", "NumBack",12));
        songs.add(new Songs("Housing", "RasL",13));
        songs.add(new Songs("Hypnotize Me", "The Artisans Beats",14));
    } else if (data.equals(getString(R.string.category_latin))) {
        // Set category
        songCategory = getString(R.string.category_latin);
        // Add songs
        songs.add(new Songs("Bony", "T.B.T ft. Bufalo El Fuete ",15));
        songs.add(new Songs("Solo Amigos", "Edward  Jay",16));
        songs.add(new Songs("Me Canse", "Harveys",17));
        songs.add(new Songs("Sola", "Mackenzie La Fuerza",18));
        songs.add(new Songs("Fallin Down", "The Madpix Project",19));
    }

    // Create an {@link SongsAdapter}, whose data source is a list of {@link Songs}. The
    // adapter knows how to create list items for each item in the list.
    SongsAdapter adapter = new SongsAdapter(this, 0, songs);

    // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
    // There should be a {@link ListView} with the view ID called list, which is declared in the
    // songs_list.xml layout file.
    final ListView listView = findViewById(R.id.list);

    // Make the {@link ListView} use the {@link SongsAdapter} we created above, so that the
    // {@link ListView} will display list items for each {@link Songs} in the list.
    listView.setAdapter(adapter);

    // Set OnClickListener on ListView to identify the item on ListView clicked by user
    // song on the ListView item clicked is passed on to player activity
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            // Getting list item index
            Intent intent = new Intent(getApplicationContext(), Player.class);
            intent.putParcelableArrayListExtra("song", songs);
            intent.putExtra("title", songs.get(position).getSongTitle());
            intent.putExtra("artist", songs.get(position).getSongArtist());
            intent.putExtra("pos", songs.get(position).getRawSong());
            intent.putExtra("index", songs.get(position));
            intent.putExtra("data", data);

            // Closing PlayListView and opening new activity
            startActivity(intent);
        }
    });

}

My button next method :

 btnNext = findViewById(R.id.btnNext);
    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            PartyMp.stop();
            PartyMp.reset();
            pos = pos + 1;
            PartyMp = MediaPlayer.create(getApplicationContext(), rawF[pos]);
            titleOfSong.setText(String.valueOf(songs.get(pos).getSongTitle()));
            artistOfSong.setText(String.valueOf(songs.get(pos).getSongArtist()));
            PartyMp.start();
        }

    });
K K
  • 11
  • 1
  • If asking about some specific `Exception`s, please always include a stack trace and highlight the line that causese the `Exception`. – Turing85 May 03 '18 at 14:48
  • `but I didn't create new array list for every category` no but you overwrite it each time `displaySongs` is called so it will never hit 20. What's with `pos = pos + 1;` in your `button next method` snippet? That's what is causing the issue. – IsThisJavascript May 03 '18 at 14:50
  • so how should I replace it? @IsThisJavascript – K K May 03 '18 at 14:52
  • Well I don't know what `pos` is or its use, where it's initially declared and what changes it. – IsThisJavascript May 03 '18 at 14:53
  • Ok I think I know, `pos` is the `10` in `songs.add(new Songs("Disc Over", "Becaysi",10));` right? If that's correct then you'll need to change the `pos` in your ArrayList constructor so they start at 0 and end at 4 instead of carrying on from the previous group. – IsThisJavascript May 03 '18 at 14:57
  • intent.putExtra("pos", songs.get(position).getRawSong());, where RawSong() are the numbers, wich you can find in my arraylist @IsThisJavascript – K K May 03 '18 at 14:57
  • thank you @IsThisJavascript ! I didn't even think about that. It works now! – K K May 03 '18 at 14:59

1 Answers1

0

Your array does actually contain no more than 5 elements. Checks like if (data.equals(getString(R.string.category_hiphop))) Do not allow you to add all elements at the same time. Your array does contain only one subset of 5 songs at the same time.

Taras
  • 1,521
  • 2
  • 9
  • 5
  • but I want to use my "data" to create category of music which user can click. – K K May 03 '18 at 14:54
  • The simplest solution possible is to use a few arrays: one to handle categories and other one holding all items. In order to get better app architecture I would recommend adding "categoryID" field to your song object, hold all data within single one list and filter it to represent category selected. – Taras May 08 '18 at 12:05