我想在我的歌曲列表活动中显示列表视图,.. 为此,我创建了 MyService 类,我将用于播放服务。在那个 MyService 类中,我创建了可以在 SDCard 上获取所有可用歌曲的方法。我想通过使用服务在歌曲列表活动的列表视图中显示所有歌曲。
这是我在 songList Activity 中的代码:
private ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
objservice = null;
}
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
objservice = ((MyService.LocalBinder) service).getService();
adapter = (ListAdapter) objservice.getSongList();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.playlist);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
bindService(new Intent(PlayListActivity.this, MyService.class),
serviceConnection, BIND_AUTO_CREATE);
setListAdapter(adapter);
}
服务类代码:
public class MyService extends Service {
private MediaPlayer mp = null;
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private ListAdapter adapter = null;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new LocalBinder();
}
@Override
public void onCreate() {
}
public class LocalBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
public ListAdapter getSongList() {
final ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
Cursor cursor = getContentResolver().query(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null, null, null, null);
if (cursor == null) {
// Query Failed , Handle error.
} else if (!cursor.moveToFirst()) {
// No media on the device.
} else {
int titleColumn = cursor
.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = cursor
.getColumnIndexOrThrow(android.provider.MediaStore.Audio.Media.DATA);
int artistcolumn = cursor
.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
int durationcolumn = cursor
.getColumnIndex(android.provider.MediaStore.Audio.Media.DURATION);
int id = cursor
.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
for (int i = 0; i < cursor.getCount(); i++) {
String thisTitle = cursor.getString(titleColumn);
String path = cursor.getString(idColumn);
String artist = cursor.getString(artistcolumn);
Long duration = cursor.getLong(durationcolumn);
Utilities objUtilities = new Utilities();
String timeDuration = objUtilities
.milliSecondsToTimer(duration);
String ID = cursor.getString(id);
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle", thisTitle);
song.put("songPath", path);
song.put("artist", artist);
song.put("duration", timeDuration);
song.put("id", ID);
songsList.add(song);
cursor.moveToNext();
}
}
for (int i = 0; i < songsList.size(); i++) {
// creating new HashMap
HashMap<String, String> song = songsList.get(i);
// adding HashList to ArrayList
songsListData.add(song);
}
String[] from = { "songTitle", "artist", "duration" };
int[] to = { R.id.songTitle, R.id.songArtist, R.id.duration };
return adapter = new SimpleAdapter(this, songsListData,
R.layout.playlist_item, from, to) {
public long getItemId(int position) {
return songsListData.indexOf(getItem(position));
}
};
}
}
那么我的错误是什么?