如何调试NotifydatasetChanged无法正常工作?

时间:2016-01-03 18:47:38

标签: android android-layout listview android-fragments android-activity

Notifydataset更改它不起作用。我搜索了很多,但似乎没有帮助。

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;
        }

我可以从播放列表中删除所需的歌曲但我的列表视图没有更新。

1 个答案:

答案 0 :(得分:1)

删除对象时,请从传递给适配器的列表中删除它们,然后调用notifyDataSetChanged()。

或者您也可以使用新修改的列表创建适配器对象并将其设置为列表。

两者都可以。希望它有所帮助。