首页 新闻 搜索 专区 学院

android ListView内容变化后的动态刷新

0
悬赏园豆:10 [已关闭问题] 关闭于 2014-02-14 17:42

代码如下:

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

更新无效,只有重新进入页面才会显示最新的数据,求指教

luckydd的主页 luckydd | 初学一级 | 园豆:77
提问于:2014-01-27 15:19
< >
分享
所有回答(2)
0

代码太短,看不出来是不是同一个listData。ListView的数据源只能使用同一个对象,因为找数据是按引用来读的,一旦重新赋值,引用就会改变。所以先要确认listData是同一个。

阿呜的边城 | 园豆:204 (菜鸟二级) | 2014-01-27 16:37
0

是当时写错了,后来自己找到了原因

luckydd | 园豆:77 (初学一级) | 2014-02-14 17:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册