0
Notifydataset改變它不起作用。我搜查了很多,但似乎沒有幫助。如何調試不工作的NotifydatasetChanged?
public class PlaylistTracksAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
public PlaylistTracksAdapter(Context context, ArrayList<HashMap<String, String>> list) {
mContext = context;
this.songsList = list;
}
@Override
public int getCount() {
return songsList.size();
}
@Override
public Object getItem(int position) {
return songsList;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.song_layout, null);
}
TextView mtxtSongName = (TextView) convertView.findViewById(R.id.song_name);
TextView mTxtSongAlbumName = (TextView) convertView.findViewById(R.id.artist_name);
TextView mTxtSongDuration = (TextView) convertView.findViewById(R.id.list_songduration);
mtxtSongName.setText(songsList.get(position).get("songName"));
mTxtSongAlbumName.setText(songsList.get(position).get("songArtist"));
try {
mTxtSongDuration.setText(getDuration(Long.parseLong(songsList.get(position).get("songDuration"))));
} catch (NumberFormatException e) {
Log.d("NumberFormatException", "" + e);
}
return convertView;
}
public static String getDuration(long millis) {
if (millis < 0) {
throw new IllegalArgumentException("Duration must be greater than zero!");
}
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
millis -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);
StringBuilder sb = new StringBuilder(6);
sb.append(minutes < 10 ? "" + minutes : minutes);
sb.append(":");
sb.append(seconds < 10 ? "" + seconds : seconds);
//sb.append(" Secs");
return sb.toString();
}
}
MainActivity.class
case DELETE_ITEM: {
removeFromPlaylist(this, mSelectedId, playListID);
adapter.notifyDataSetChanged();
return true;
}
我能夠刪除播放列表中的想要的歌曲,但我的列表視圖沒有更新。
確保「PlayListTracksAdapter」中'songsList'的內容實際上已被修改。您可能正在更改列表副本的內容,或者在調用'notifyDataSetChanged()'之前沒有重新加載列表。 – Karakuri
嘗試更改getItemId以返回位置而不是0,getItem返回songsList.get(位置)而不是songsList。如果這不起作用,請在問題中加入removeFromPlaylist方法。 – Jahnold
您的最新問題與可讀性有關,以及有人會僅僅解決它的期望有一些問題。想象一下,當我花幾分鐘修復它時,我感到非常沮喪,只有當我幾乎完成後才發現它已被刪除!故事的道德:儘可能清楚地寫出問題,尋求幫助,而不是期待別人爲你做。避免要求緊急/儘快!謝謝。 – halfer