以下是该方案。
1)我有一个listview,它会生成所有歌曲的列表(这是一个片段)
2)现在点击任何一首歌后,就会启动一项新活动。
3)这是我在一些时间之后从新活动回来时遇到的问题(请注意,当我立即回来时不会发生这种情况)我发现这个清单已经重复了 当我点击任何项目时,它都不起作用。
我不知道为什么会发生这种情况,我能做些什么来避免它。
代码:
public class F_Songs extends Fragment implements Serializable {
ListView SngList;
ArrayList<SongDetails> songdetails = new ArrayList<SongDetails>();
String[] TRACK_COLUMNS;
Cursor songCursor = null;
Adapter_ListView ab;
int index;
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = (ViewGroup) inflater.inflate(R.layout.l_songs, null);
TRACK_COLUMNS = new String[] { MediaStore.Audio.AudioColumns.ALBUM, MediaStore.Audio.AudioColumns.ARTIST,
MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME,
MediaStore.Audio.AudioColumns.DURATION, MediaStore.Audio.Media._ID, };
String sortOrder = "REPLACE ('<BEGIN>' || " + MediaStore.Audio.Media.TITLE + ", '<BEGIN>The ', '<BEGIN>')"
+ " COLLATE NOCASE ASC";
// MediaStore.Audio.Media.TITLE + " COLLATE NOCASE ASC";
Cursor songCursor = getActivity().managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, TRACK_COLUMNS,
MediaStore.Audio.Media.DURATION + ">= 10000", null, sortOrder);
if (songCursor != null) {
while (songCursor.moveToNext()) {
SongDetails songs = new SongDetails();
songs.song = songCursor.getString(8);
songs.Path = songCursor.getString(2);
songs.Album = songCursor.getString(0);
songs.Artist = songCursor.getString(1);
songdetails.add(songs);
}
}
SngList = (ListView) view.findViewById(R.id.list);
registerForContextMenu(SngList);
/*
* String uri = "android.resource://" +
* this.getActivity().getPackageName() + "/"+R.raw.ro; SongDetails
* songs = new SongDetails(); songs.setPath2( uri) ;
* songs.setSong("ro"); songdetails.add(songs);
*/
SngList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView a, View v, int position, long id) {
Intent intent = new Intent(getActivity(), Player.class);
intent.putParcelableArrayListExtra("Data1", songdetails);
intent.putExtra("Data2", position);
startActivity(intent);
}
});
ab = new Adapter_ListView(songdetails);
SngList.setAdapter(ab);
}
}
答案 0 :(得分:1)
很难找到问题的来源。我的猜测是因为这条线
ArrayList<SongDetails> songdetails = new ArrayList<SongDetails>();
在onCreateView
方法之外。
尝试使用MVC模式和Singleton改进数据管理,就像我在上一个问题中建议的那样。