zoukankan      html  css  js  c++  java
  • 2021.3.17 ListView更新1

    一、今日学习内容

      

    删除某一项

    同样的,我们写两个方法,一个直接删对象,一个根据游标来删:

    public void remove(Data data) {
        if(mData != null) {
            mData.remove(data);
        }
        notifyDataSetChanged();
    }
    
    public void remove(int position) {
        if(mData != null) {
            mData.remove(position);
        }
        notifyDataSetChanged();
    }

    然后加两个Button,调用下这两个方法:

    case R.id.btn_remove:
        mAdapter.remove(mData_5);
        break;
    case R.id.btn_remove2:
        mAdapter.remove(2);
        break;

    运行效果图

    从图中我们可以看到,第五项被移除了,然后点击游标删除数据,一直删的是第三项!


    4.移除所有的记录:

    这个更加简单,直接调用clear方法即可!方法代码如下:

    public void clear() {
        if(mData != null) {
            mData.clear();
        }
        notifyDataSetChanged();
    }

    5.更新某一个记录

    细心的你应该发现了,进行了数据修改操作后,都会调用一个notifyDataSetChanged(); 一开始我以为:

    notifyDataSetChanged()会把界面上现实的的item都重绘一次,这样会影响ui性能吧,如果数据量 很大,但是我改变一项就要重新绘制所有的item,这肯定不合理是吧!于是乎,我用了一个傻办法 来修改某个Item中控件的值,我在Java代码中写了这样一段代码:

    private void updateListItem(int postion,Data mData){
        int visiblePosition = list_one.getFirstVisiblePosition();
        View v = list_one.getChildAt(postion - visiblePosition);
        ImageView img = (ImageView) v.findViewById(R.id.img_icon);
        TextView tv = (TextView) v.findViewById(R.id.txt_content);
        img.setImageResource(mData.getImgId());
        tv.setText(mData.getContent());
    }
  • 相关阅读:
    【python 待做】
    【python 第13日】网络通信socket socketserver
    【python第11日】自动生成项目目录 安全插入本文件夹下文件,即使别人引用也可以
    【python 第12日】 except异常
    【python 第10日】打飞机的小游戏 pygame
    【python 第9日】上下文 类装饰器 元类 属性描述符扩展
    Python(2.7)-字符串
    Python(2.7)-字典(dict)
    Python(2.7)-列表(list)
    mysql linux centos yum 安装
  • 原文地址:https://www.cnblogs.com/wmdww/p/14903833.html
Copyright © 2011-2022 走看看