Android -- CursorAdapter

CursorAdapter                                                                       

CursorAdapter继承于BaseAdapter,它是个虚类,它为cursor和ListView提供了连接的桥梁。  

public abstract class CursorAdapter extends BaseAdapter

注意cursor的必须要有个命名为"_id"的列。比如Contacts._ID就为"_id"

必须实现以下函数:

abstract View newView(Context  context, Cursor  cursor, ViewGroup  parent)
    
abstract void  bindView(View  view, Context  context, Cursor  cursor)

newView该函数第一次回调用后,如果数据增加后也会再调用,但是重绘是不会调用的
数据增加后,回调用该函数来生成与新增数据相对应的view。
bindView函数第一次回调用后,如果数据更新也会再调用,但重绘会再次调用的

总的来说应该是在调用bindView如果发现view为空会先调用newView来生成view。

Code                                                                                   

复制代码
public class MySqliteOpenhelper extends SQLiteOpenHelper 
{                                                                    
    public MySqliteOpenhelper(Context context,  int version) 
    { 
        super(context, "dianhuaben.db", null, version); 
    } 
    @Override
    public void onCreate(SQLiteDatabase db) 
    {//注意:使用CursorAdapter时,创建表必须有以_id为列名的列 
        String sql = "CREATE TABLE dhb (_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))"; 
        db.execSQL(sql); 
    } 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    { 
    } 
}
复制代码
复制代码
public void createCursorAdapter(Cursor cursor) 
    { //游标适配器,构造方法,传入cursor 
        mAdapter = new CursorAdapter(this, cursor) 
        {//重写两个方法 
            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent) 
            {//找到布局和控件 
                ViewHolder holder = new ViewHolder(); 
                LayoutInflater inflater = getLayoutInflater(); 
                View inflate = inflater.inflate(R.layout.listview_item, null); 
                holder.item_tv_name = (TextView) inflate.findViewById(R.id.item_tv_name); 
                holder.item_tv_phone = (TextView) inflate.findViewById(R.id.item_tv_phone); 
                inflate.setTag(holder); 
                return inflate;//返回的view传给bindView。 
            } 
                                                              
            @Override
            public void bindView(View view, Context context, Cursor cursor) 
            {
//                把数据设置到界面上 
                ViewHolder holder = (ViewHolder) view.getTag(); 
                String name = cursor.getString(cursor.getColumnIndex("name")); 
                String phone = cursor.getString(cursor.getColumnIndex("phone")); 
                holder.item_tv_name.setText(name); 
                holder.item_tv_phone.setText(phone); 
            } 
                                                          
        }; 
                                                    
    };
复制代码

SimpleCursorAdapter                                                                

简单提及一下

复制代码
public void createSimpleCursorAdapter(Cursor cursor) 
    {// SimpleCursorAdapter继承了CursorAdapter继承了BaseAdapter 
        String[] from = {TABLE_NAME_NAME,TABLE_NAME_PHONE};//列名与控件id一一对应 
        int[] to = {R.id.item_tv_name,R.id.item_tv_phone}; 
        //用的是SimpleCursorAdapter,用法和simpleAdapter相似 
        mAdapter = new SimpleCursorAdapter(MainActivity.this, R.layout.listview_item, cursor, from, to); 
    }
复制代码

使用SimpleCursorAdapter,时,创建表必须有以_id为列名的列

我是天王盖地虎的分割线                                                             

posted @   我爱物联网  阅读(1244)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
AmazingCounters.com
点击右上角即可分享
微信分享提示