继承SimpleCursorAdapter定制Adapter

来源:互联网 发布:票乎为什么停止运营 编辑:程序博客网 时间:2024/06/05 22:46

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

[java] view plaincopy
  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 plaincopy
  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
原创粉丝点击