gpt4 book ai didi

android - 从服务类 Android 设置 ListView 数据

转载 作者:太空狗 更新时间:2023-10-29 14:23:45 25 4
gpt4 key购买 nike

我想在我的歌曲列表 Activity 中显示 ListView ,.. 为此,我创建了我将用于播放服务的 MyService 类。在那个 MyService 类中,我创建了方法来获取 SDCard 上可用的所有歌曲。我想通过使用服务在歌曲列表 Activity 的 ListView 中显示所有那首歌。

这是我在 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));
}
};
}
}

那么我的错误是什么?

最佳答案

我的问题已经解决了。我创建了一种将适配器设置为我的 ListView 的方法。我已将该方法称为 ater 连接到我的服务:

public void updateListView() {
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

int songIndex = (int) id;
// Starting new intent
Intent in = new Intent(getApplicationContext(),
AndroidBuildingMusicPlayerActivity.class);
// Sending songIndex to PlayerActivity
in.putExtra("songIndex", songIndex);
in.putExtra("listFlag", 0);
startActivity(in);

}
});
}

在这里调用这个方法:

public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
objservice = ((MyService.LocalBinder) service).getService();
adapter = (ListAdapter) objservice.getSongList();
updateListView(); // here was my problem
}

这是您可以使用服务更新 ListView 的方式。谢谢。

关于android - 从服务类 Android 设置 ListView 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14273608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com