使用ArrayAdapter外部数组中对象的位置

时间:2016-09-06 05:26:20

标签: android android-arrayadapter mediacontroller

我已经创建了一个音频播放应用。我已经创建了一个ListView来列出歌曲并将歌曲添加到一个阵列中,并且当点击列表中的项目时可以播放歌曲。

现在我想在应用中使用内置的MediaController。我在实现在下一首和上一首曲目之间切换的方法时遇到问题,因为我无法引用正在播放的曲目的位置。

这是java文件

    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(){

        }
    }

实现此目的的正确方法是什么?

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>

1 个答案:

答案 0 :(得分:2)

在班级中全球宣布职位.....

private int position=0;

并使用这个......

@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? ------
        }

注意: - 位置维持哪些歌曲想要播放以及上述条件解决NPE和ArrayOutOfIndex异常......