代码如下:
Activity: List<SerializableUtil> listData = this.playlistMgr .getPlayListAudioList(); this.listData.addAll(listData); this.playListAdapter = new PlayListAdapter(this.thisActivity, this.listData); this.playListView.setAdapter(playListAdapter); this.playListAdapter.notifyDataSetChanged();
PlayListAdapter extends BaseAdapter: public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = View.inflate(mContext, R.layout.item_playlist, null); holder.playImageView = (ImageView) convertView .findViewById(R.id.item_playlist_imageview); holder.playTitleTextView = (TextView) convertView .findViewById(R.id.item_playlist_title_textview); holder.playDownloadButtonView = (Button) convertView .findViewById(R.id.item_playlist_download); holder.playDeleteButtonView = (Button) convertView .findViewById(R.id.item_playlist_delete); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.playImageView.setVisibility(View.INVISIBLE); final AudioInfo audioInfo=(AudioInfo)this.listData.get(position); holder.playTitleTextView.setText(audioInfo.getTitle()); holder.playDownloadButtonView.setBackgroundResource(R.drawable.player_playlist_cache_highlighted_button_green); holder.playDeleteButtonView.setBackgroundResource(R.drawable.player_playlist_delete_highlighted_button_green); holder.playDeleteButtonView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(DBManager.getInstance(mContext).playListMgr.deleteByAudioId(audioInfo.getAudioId())){ listData.remove(audioInfo.getAudioId()); notifyDataSetChanged(); Log.e("wenyiting","delete"+listData.size()); } } }); if (position % 2 == 0) { convertView.setBackgroundColor(Color.parseColor("#ededed")); } else { convertView.setBackgroundColor(Color.TRANSPARENT); } return convertView; }
更新无效,只有重新进入页面才会显示最新的数据,求指教
代码太短,看不出来是不是同一个listData。ListView的数据源只能使用同一个对象,因为找数据是按引用来读的,一旦重新赋值,引用就会改变。所以先要确认listData是同一个。
是当时写错了,后来自己找到了原因