Search code examples
androidandroid-arrayadaptermediacontroller

Using position of objects in an array outside ArrayAdapter


I've created an app for audio playback. I've created a ListView to list the songs and added the songs to an array and am abe to play the songs when clicking on the items in the list.

Now I want to use the in-built MediaController with the app. I'm having issues in implementing methods to switch between next and previous tracks because I'm unable to reference the position of the track being played.

Here's the java file

    public class AlternateActivity extends AppCompatActivity implements MediaPlayerControl, AdapterView.OnItemClickListener {

    private ListView songView;
        private MediaPlayer mediaPlayer;
        private final String[] songList = {"Song 1", "Song 2", "Song 3", "Song 4", "Song 5"};

        private final int[] songID = {R.raw.track1, R.raw.track2, R.raw.track3, R.raw.track4, R.raw.track5};



        private MusicController musicController;

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

            mediaPlayer = new MediaPlayer();

            songView = (ListView) findViewById(R.id.song_list);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, songList);
            songView.setAdapter(adapter);

            songView.setOnItemClickListener(this);

            setMusicController();


        };



        public void playSong(int position){
            mediaPlayer.reset();
            mediaPlayer = MediaPlayer.create(getApplicationContext(), songID[position]);
            mediaPlayer.start();

        }

        private void setMusicController(){
            musicController = new MusicController(this);
            musicController.setPrevNextListeners(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    playNext();
                }
            }, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    playPrev();
                }
            });

            musicController.setMediaPlayer(this);
            musicController.setAnchorView(findViewById(R.id.song_list));
            musicController.setEnabled(true);
        }

    @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            playSong(position);
        }


        public void playPrev(){

        }

        public void playNext(){

        }
    }

What is the correct way of implementing this?

Layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#FFAA50"
    tools:context=".AlternateActivity" >

    <ListView
        android:id="@+id/song_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

Solution

  • declare position globally in class .....

    private int position=0;
    

    and use this .....

    @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                this.position=position;
                playSong(position);
            }
    
    
            public void playPrev(){
    using "songID--;" gives error "Cannot assign a value to final variable songID"
                     if(position>0)
                      playSong(--position);
                     ----- what to do here? ------
            }
    
            public void playNext(){
                      if(position<(songList.length-1))
                      playSong(++position);
                      ----- what to do here? ------
            }
    

    Note:- Position is maintain which songs want to play AND above condition solve NPE and ArrayOutOfIndex Exception...