做了下MP3播放器,扫描歌曲后无法显示到ListView中

日落日出__ 2014-02-05 08:23:43
package com.example.mymp3player;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class FristListActivity extends Activity{
private ListView MusicList ;
private List<Mp3Info> mp3Infos ;
private Button music_start ;
private Button music_repeat ;
private Button music_previous ;
private Button music_next ;
private Button music_shuffle ;
private TextView music_title ;
private TextView music_times ;
private ImageView music_album ;
private Button music_menu ;
private SimpleAdapter listAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.first_list_music);
MusicList = (ListView)findViewById(R.id.music_list);
music_start = (Button)findViewById(R.id.music_star);
music_repeat = (Button)findViewById(R.id.music_repeat);
music_previous = (Button)findViewById(R.id.music_previous);
music_next = (Button)findViewById(R.id.music_next);
music_shuffle = (Button)findViewById(R.id.music_shuffle);
music_title = (TextView)findViewById(R.id.music_playingTitle);
music_times = (TextView)findViewById(R.id.music_playingtimes);
music_album = (ImageView)findViewById(R.id.music_album);
music_menu = (Button)findViewById(R.id.music_menu);
mp3Infos = getMp3Infos();
setListAdapter(MusicList,mp3Infos);
MusicList.setVisibility(MusicList.VISIBLE);

}
public List<Mp3Info> getMp3Infos(){
Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
List<Mp3Info> Mp3Infos = new ArrayList<Mp3Info>();
for(int i = 0;i < cursor.getCount();i++){
Mp3Info mp3Info = new Mp3Info();
cursor.moveToNext();
long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)); //音乐ID
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));//音乐标题
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));//歌手
long times = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));//音乐时长
long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));//音乐大小
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));//音乐路径
int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));//判断是否为音乐,0为不是 .
//把音乐添加到List集合中
if(isMusic != 0){
mp3Info.setMusic_id(id);
mp3Info.setMusic_title(title);
mp3Info.setMusic_artist(artist);
mp3Info.setMusic_times(times);
mp3Info.setMusic_size(size);
mp3Info.setMusic_path(path);
}
}
return Mp3Infos;
}

public void setListAdapter(ListView musicList,List<Mp3Info> mp3Infos){
List<HashMap<String,String>> mp3list = new ArrayList<HashMap<String,String>>();
for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();){
Mp3Info mp3Info = (Mp3Info)iterator.next();
HashMap<String,String> hashMap = new HashMap<String,String>();
hashMap.put("title", mp3Info.getMusic_title());
hashMap.put("artist", mp3Info.getMusic_artist());
hashMap.put("time", String.valueOf(mp3Info.getMusic_times()));
hashMap.put("size", String.valueOf(mp3Info.getMusic_size()));
hashMap.put("path", mp3Info.getMusic_path());
mp3list.add(hashMap);
}
listAdapter = new SimpleAdapter(FristListActivity.this, mp3list, R.layout.list_item_layout, new String[]{"title","artist","time"}, new int[]{R.id.music_title,R.id.music_artist,R.id.music_times});
musicList.setAdapter(listAdapter);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}



求大神帮我看下,,,小弟才学不久,搞不清哪里出了问题
...全文
5309 点赞 收藏 3
写回复
3 条回复
dai_jiawei 2014年05月25日
这么一堆确实没人愿意细看。。
回复 点赞
ohfiner 2014年02月05日
把问题描述清楚,对应代码简单分析下,什么错误说错啦。 就一堆没格式的代码,没人愿意看
回复 点赞
日落日出__ 2014年02月05日
求大神帮帮忙
回复 点赞
发动态
发帖子
Eclipse
创建于2007-09-28

3.1w+

社区成员

4.9w+

社区内容

Java Eclipse
社区公告
暂无公告