继承SimpleCursorAdapter定制Adapter

以前对newview 和bindview的理解不是很深,今天特意研究研究。

[java]  view plain copy
  1.  newView    0  
  2. bindView    0  
  3.  newView    1  
  4.  bindView    1  
  5.  newView    2  
  6.  bindView    2  
  7. newView    3  
  8.  bindView    3  
  9. newView    4  
  10.  bindView    4  
  11.  newView    5  
  12.  bindView    5  
  13.  newView    6  
  14.  bindView    6  
  15.  newView    7  
  16.  bindView    7  

开始时有多少可以显示的view就执行下多少下newView,后面滚动listview的时候 只执行bindView,除非又可以看到新的item如上面的如果可以看到第8个item的时候,那就要newView.否则都不执行newView,下面是listview滚动的时候的执行log.

 

[java]  view plain copy
  1. bindView    8  
  2. bindView    9  
  3. bindView    10  
  4. bindView    11  
  5. bindView    12  
  6. bindView    13  
  7. bindView    14  
  8. bindView    15  
 


一般情况SimapleCursorAdapter是用来和数据库有关cursor。一般newView中只负责View的部分,setTag(View)-->只对view,对数据的处理都放在bindView中,不然会有异常情况出现!因为newView在listView滚动的情况下大部分时间不执行,除非上面所说的!总的来说android的用这个就实现了界面更新时候只是更新了数据。而view还是和以前一样。


SimpleCursorAdapter是一个简单的adapter,提供数据库Cursor到TextView的映射。

在实际开发过程中,除了TextView外,往往还需要依赖于数据库数据的其它的组件。

通过继承SimpleCursorAdapter,重写bindView(View view, Context context, Cursor cursor)来实现


public View newView(Context context, Cursor cursor, ViewGroup parent)
07. {//找到布局和控件
08. ViewHolder holder = new ViewHolder();
09. LayoutInflater inflater = getLayoutInflater();
10. View inflate = inflater.inflate(R.layout.listview_item, null);
11. holder.item_tv_name = (TextView) inflate.findViewById(R.id.item_tv_name);
12. holder.item_tv_phone = (TextView) inflate.findViewById(R.id.item_tv_phone);
13. inflate.setTag(holder);
14. return inflate;//返回的view传给bindView。
15. }
16.  
17. @Override
18. public void bindView(View view, Context context, Cursor cursor)
19. {//复用布局。www.it165.net
20. //                把数据设置到界面上
21. ViewHolder holder = (ViewHolder) view.getTag();
22. String name = cursor.getString(cursor.getColumnIndex("name"));
23. String phone = cursor.getString(cursor.getColumnIndex("phone"));
24. holder.item_tv_name.setText(name);
25. holder.item_tv_phone.setText(phone);
26. }

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值