ソングリスト アクティビティでリストビューを表示したい..そのために、再生サービスに使用する MyService クラスを作成しました。その MyService クラスで、SDCard で利用可能なすべての曲を取得するメソッドを作成しました。サービスを利用してsonglist Activityのリストビューにその曲を全て表示したい。
これが 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));
}
};
}
}
それで、私のエラーは何ですか?