如何从列表视图中的项目单击中获取歌曲在android中
Posted
技术标签:
【中文标题】如何从列表视图中的项目单击中获取歌曲在android中【英文标题】:How to get song from list view in item click in android 【发布时间】:2017-05-17 16:28:02 【问题描述】:我制作了 android 应用程序,在其中我从移动存储读取所有音频歌曲并添加所有歌曲以自定义列表视图。在我的列表视图中,有一个简单的文本视图,其中放置了歌曲现在我想将点击侦听器添加到列表中,并从列表视图中获取这些歌曲并传递给另一个活动以播放歌曲。我有两节课 MainActivity.java
public class MainActivity extends AppCompatActivity
ListView lv;
String[] items;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lvPlaylist);
final ArrayList<File> mySongs = findSongs(Environment.getExternalStorageDirectory());
items = new String[mySongs.size()];
for (int i = 0; i < mySongs.size(); i++)
//toast(mySongs.get(i).getName().toString());
items[i] = mySongs.get(i).getName().toString().replace(".mp3", "").replace(".wav", "");
ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(), R.layout.song_layout, R.id.textView, items);
lv.setAdapter(adp);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l)
Intent intent = new Intent(MainActivity.this,Player.class);
intent.putExtra("pos",position);
intent.putExtra("songList",mySongs);
startActivity(intent);
// startActivity(new Intent(getApplicationContext(),Player.class).putExtra("pos",position).putExtra("songlist",mySongs));
);
public ArrayList<File> findSongs(File root)
ArrayList<File> al = new ArrayList<File>();
File[] files = root.listFiles();
for (File singleFile : files)
if (singleFile.isDirectory() && !singleFile.isHidden())
al.addAll(findSongs(singleFile));
else
if (singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav"))
al.add(singleFile);
return al;
public void toast(String text)
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
这里是 Palyer.java
public class Player extends AppCompatActivity implements View.OnClickListener
static MediaPlayer mp;
ArrayList mySongs;
int position;
Uri u;
Thread updateSeekBar;
SeekBar sb;
Button btPlay, btFF, btFB, btPv, btNxt;
MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
btPlay = (Button)findViewById(R.id.btPlay);
btFF = (Button)findViewById(R.id.btFF);
btFB = (Button)findViewById(R.id.btFB);
btPv = (Button)findViewById(R.id.btPv);
btNxt = (Button)findViewById(R.id.btNxt);
btPlay.setOnClickListener(this);
btFF.setOnClickListener(this);
btFB.setOnClickListener(this);
btPv.setOnClickListener(this);
btNxt.setOnClickListener(this);
sb = (SeekBar)findViewById(R.id.seekBar);
updateSeekBar = new Thread()
@Override
public void run()
//super.run();
int totalDuration = mp.getDuration();
int currentPosition = 0;
sb.setMax(totalDuration);
while(currentPosition<totalDuration)
try
sleep(500);
currentPosition= mp.getCurrentPosition();
sb.setProgress(currentPosition);
catch(InterruptedException e)
e.printStackTrace();
;
if(mp!=null)
mp.stop();
mp.release();
Intent i = getIntent();
mySongs.add(i.getStringExtra("songList"));
position=i.getIntExtra("pos",0);
u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(),u );
mp.release();
mp.prepareAsync();
mp.start();
sb.setMax(mp.getDuration());
updateSeekBar.start();
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b)
@Override
public void onStartTrackingTouch(SeekBar seekBar)
@Override
public void onStopTrackingTouch(SeekBar seekBar)
mp.seekTo(seekBar.getProgress());
);
@Override
public void onClick(View view)
int id = view.getId();
switch (id)
case R.id.btPlay:
if(mp.isPlaying())
btPlay.setText(">");
mp.pause();
else
mp.start();
btPlay.setText("||");
break;
case R.id.btFF:
mp.seekTo(mp.getCurrentPosition()+5000);
break;
case R.id.btFB:
mp.seekTo(mp.getCurrentPosition()-5000);
break;
case R.id.btNxt:
mp.stop();
mp.release();
position = (position+1)%mySongs.size();
u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(),u );
mp.start();
sb.setMax(mp.getDuration());
break;
case R.id.btPv:
mp.stop();
mp.release();
position = (position-1<0)? mySongs.size()-1: position-1;
/*if(position-1<0)
position = mySongs.size()-1;
else
position = position-1;
*/
u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(),u );
mp.start();
sb.setMax(mp.getDuration());
break;
当我启动我的应用程序并单击列表视图中的歌曲时,我的应用程序崩溃了,任何人都可以帮助从项目单击侦听器中的列表视图中获取歌曲 谢谢。
【问题讨论】:
发布错误logcat。 那里显示错误 mySongs.add(i.getStringExtra("songList")); 添加您的崩溃日志以进行澄清 【参考方案1】: Make your songs arraylist serializable as:
public class SongsList implements Serializable
/// getter and setter
From Songs Files add object to SongList class then send this class
as:
Intent intent = new Intent(SourceActivity.this,
argetActivity.class);
intent.putExtra("SongListExtra", songList);
On receiving activity:
ArrayList<SongList> songlist = new ArrayList<SongList>();
songlist = (ArrayList<SongList>)
getIntent().getSerializableExtra("SongListExtra");
Instead of sending songList to player activity you can send that
particular song details on list item clicked according to position.
Hope it helps..
【讨论】:
以上是关于如何从列表视图中的项目单击中获取歌曲在android中的主要内容,如果未能解决你的问题,请参考以下文章