android ListView常用知识总结

发布时间:2020-07-01 12:03:16 作者:王宇斌
来源:网络 阅读:914


先来看下项目主要内容:


android ListView常用知识总结


ListView中填充数据:

  1. 重现添加数据后置顶,具体阐明了决解方案,如下:

    android ListView常用知识总结


  2. 刷新适配器后没有响应的错误现象,具体阐明了决解方案,如下:

    android ListView常用知识总结

    android ListView常用知识总结



  3. 正确示范一:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    /**
    * 正确示范一(正确运用,修改原始对象<Activity里面>对应引用<Adapter里面>也改变)
    *
    * @author johnny
    *
    */
    publicclassThreeListViewActivity extendsActivity {
    privateListView mContentLv;
    privateOneAdapter adapter;
    privateArrayList<HashMap<String, String>> arrayList=newArrayList<HashMap<String,String>>();
    @Override
    protectedvoidonCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listview);
    mContentLv = (ListView) findViewById(R.id.lv_content);
    adapter=newOneAdapter(this,arrayList);
    mContentLv.setAdapter(adapter);
    initData();
    ToastUtils.show(getApplicationContext(), "6秒后延迟添加,刷新adapter");
    //开启异步线程
    setData();
    }
    privatevoidsetData() {
    newThread(newRunnable() {
    @Override
    publicvoidrun() {
    // TODO Auto-generated method stub
    try{
    //做一些耗时的操作后添加数据,之后刷新adapter
    Thread.sleep(6000);
    } catch(InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    //可能这个时候才重网络上获取到了新数据,,现在开始添加数据
    HashMap<String, String> hashMap;
    for(inti = 0; i < 5; i++) {
    hashMap = newHashMap<String, String>();
    if(i % 4== 0) {
    hashMap.put("name", "大海");
    hashMap.put("address", "上海");
    } elseif(i % 4== 1) {
    hashMap.put("name", "老马");
    hashMap.put("address", "深圳");
    } elseif(i % 4== 2) {
    hashMap.put("name", "小三");
    hashMap.put("address", "东莞");
    } elseif(i % 4== 3) {
    hashMap.put("name", "老哥");
    hashMap.put("address", "北京");
    }
    arrayList.add(hashMap);
    }
    handler.sendEmptyMessage(1);
    }
    }).start();
    }
    Handler handler = newHandler(){
    publicvoidhandleMessage(android.os.Message msg) {
    adapter.notifyDataSetChanged();//没有重现设置adapter,只做了刷新数据,listview不会到顶。
    ToastUtils.show(getApplicationContext(), "adapter共有"+adapter.getAllList().size()+"个数据,activity共有"+arrayList.size()+"个数据,刷新结束!");
    };
    };
    privatevoidinitData() {
    HashMap<String, String> hashMap;
    for(inti = 0; i < 15; i++) {
    hashMap = newHashMap<String, String>();
    if(i % 4== 0) {
    hashMap.put("name", "小明");
    hashMap.put("address", "上海");
    } elseif(i % 4== 1) {
    hashMap.put("name", "老马");
    hashMap.put("address", "深圳");
    } elseif(i % 4== 2) {
    hashMap.put("name", "小三");
    hashMap.put("address", "东莞");
    } elseif(i % 4== 3) {
    hashMap.put("name", "老哥");
    hashMap.put("address", "北京");
    }
    arrayList.add(hashMap);
    }
    }
    }


    正确示范二:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    /**
    * 正确示范二(正确运用,Activity里面对象不用作Adapter原始对象,只做数据的备份。
    * 这也是本人比较喜欢的写法,原因:很多时候我们会不经意间改变Activity里面的数据源后导致ListView的改变,
    * 导致出错后排除错误难解<正确示范一,当然也有好处,比如:改变Activity数据后,不需要再做多余的Adapter数据的更改,方便>)
    *
    * @注意 这里只是数据添加方案本人赞成写法,具体完整结合adapter请移步个人推荐一或二<有错误示范,才能慢慢完善改变,错误何尝不是一种成长>
    * @author johnny
    *
    */
    publicclassFourListViewActivity extendsActivity {
    privateListView mContentLv;
    privateOneAdapter adapter;
    @Override
    protectedvoidonCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listview);
    mContentLv = (ListView) findViewById(R.id.lv_content);
    adapter=newOneAdapter(this);
    mContentLv.setAdapter(adapter);
    initData();
    ToastUtils.show(getApplicationContext(), "6秒后延迟添加,刷新adapter");
    //开启异步线程
    setData();
    }
    privatevoidsetData() {
    newThread(newRunnable() {
    @Override
    publicvoidrun() {
    // TODO Auto-generated method stub
    try{
    //做一些耗时的操作后添加数据,之后刷新adapter
    Thread.sleep(6000);
    } catch(InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    ArrayList<HashMap<String, String>> arrayList=newArrayList<HashMap<String,String>>();
    //可能这个时候才重网络上获取到了新数据,,现在开始添加数据
    HashMap<String, String> hashMap;
    for(inti = 0; i < 5; i++) {
    hashMap = newHashMap<String, String>();
    if(i % 4== 0) {
    hashMap.put("name", "大海");
    hashMap.put("address", "上海");
    } elseif(i % 4== 1) {
    hashMap.put("name", "老马");
    hashMap.put("address", "深圳");
    } elseif(i % 4== 2) {
    hashMap.put("name", "小三");
    hashMap.put("address", "东莞");
    } elseif(i % 4== 3) {
    hashMap.put("name", "老哥");
    hashMap.put("address", "北京");
    }
    //这里添加的只是在临时数据存储的对象,adapter里面不属于同一个对象
    arrayList.add(hashMap);
    }
    //和正确范例一区别在于此,每次都需要把数据copy到adapter里面
    adapter.setAddList(arrayList);
    handler.sendEmptyMessage(1);
    }
    }).start();
    }
    Handler handler = newHandler(){
    publicvoidhandleMessage(android.os.Message msg) {
    adapter.notifyDataSetChanged();//没有重现设置adapter,只做了刷新数据,ListView不会到顶。
    ToastUtils.show(getApplicationContext(), "adapter共有"+adapter.getAllList().size()+"个数据。");
    };
    };
    privatevoidinitData() {
    ArrayList<HashMap<String, String>> arrayList=newArrayList<HashMap<String,String>>();
    HashMap<String, String> hashMap;
    for(inti = 0; i < 15; i++) {
    hashMap = newHashMap<String, String>();
    if(i % 4== 0) {
    hashMap.put("name", "小明");
    hashMap.put("address", "上海");
    } elseif(i % 4== 1) {
    hashMap.put("name", "老马");
    hashMap.put("address", "深圳");
    } elseif(i % 4== 2) {
    hashMap.put("name", "小三");
    hashMap.put("address", "东莞");
    } elseif(i % 4== 3) {
    hashMap.put("name", "老哥");
    hashMap.put("address", "北京");
    }
    arrayList.add(hashMap);
    }
    adapter.setAddList(arrayList);
    }
    }



    ListView 适配器推荐写法:


    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      @Override
      publicView getView(intposition, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
      View v = convertView;
      //判断是否为null,这是每个要做复用的都要写的
      if(v == null) {
      v = inflater.inflate(R.layout.lv_item, null);
      }
      //相比之下
      //写法太集中,没有细化
      /*if (null == convertView) {
      holder = new ViewHolder();
      convertView = inflater.inflate(R.layout.lv_item, null);
      holder.mNameTv = (TextView) convertView.findViewById(R.id.tv_name);
      holder.mAddressTv = (TextView) convertView.findViewById(R.id.tv_address);
      convertView.setTag(holder);
      } else {
      holder = (ViewHolder) convertView.getTag();
      }*/
      //简洁,把初始化控件放到了ViewHolder里面,是的getView方法更清晰简洁,只需要做数据的赋值和复杂的逻辑,没有混为一滩
      ViewHolder holder = (ViewHolder) v.getTag();    
      if(holder == null) {
      holder = newViewHolder(v);
      v.setTag(holder);
      }
      HashMap<String, String> hashMap = getItem(position);
      holder.mNameTv.setText(hashMap.get("name"));
      holder.mAddressTv.setText(hashMap.get("address"));
      returnv;
      }



    方法二:

    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      @Override
      publicView getView(intposition, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
      View v = convertView;
      //判断是否为null,这是每个要做复用的都要写的
      if(v == null) {
      v = inflater.inflate(R.layout.lv_item, null);
      }
      //简洁,方便,快速
      TextView mNameTv=ViewHolder.get(v, R.id.tv_name);  
      TextView mAddressTv=ViewHolder.get(v, R.id.tv_address);  
      HashMap<String, String> hashMap = getItem(position);
      mNameTv.setText(hashMap.get("name"));
      mAddressTv.setText(hashMap.get("address"));
      returnv;
      }




ListView中疑难杂症:

只做截图具体下载代码查看:不需要豆子


eandroid ListView常用知识总结

android ListView常用知识总结


另链接单选删除帖子效果如下:

android ListView常用知识总结

地址:点击进入


×××:

ListView中常用知识总结


推荐阅读:
  1. android基础之ListView
  2. Android 学习笔记--android——listview总结

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

listview 知识汇总 listview适配器

上一篇:expdp在rac下parallel并行报错解决

下一篇:程序员的简单接口压测

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》