【问题标题】:How to debug a NotifydatasetChanged that is not working?如何调试不起作用的 NotifydatasetChanged?
【发布时间】:2016-01-03 18:47:37
【问题描述】:

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

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

【问题讨论】:

  • 确认PlayListTracksAdaptersongsList的内容确实被修改了。您可能正在更改列表副本的内容,或者在进行更改之后和调用 notifyDataSetChanged() 之前没有重新加载列表。
  • 尝试将 getItemId 更改为返回位置而不是 0,并将 getItem 更改为返回歌曲列表.get(位置)而不是歌曲列表。如果这不起作用,请在问题中包含您的 removeFromPlaylist 方法。
  • 您的最新问题存在许多与可读性相关的问题,并且期望有人会解决它。想象一下当我花了几分钟修复它时我的沮丧,却发现它在我快完成时被删除了!故事的寓意:尽可能清楚地写出问题,并寻求帮助,而不是指望有人为你做。也不要要求紧急/尽快等!谢谢。

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


【解决方案1】:

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

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

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

【讨论】:

  • 说真的,你是救生员,非常感谢,谢谢.....我是从数据库中删除它,但不是从列表中删除,所以它没有改变。
猜你喜欢
  • 2014-03-03
  • 1970-01-01
  • 1970-01-01
  • 2016-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多