• 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为列名的列

    我是天王盖地虎的分割线                                                             

  • 相关阅读:
    09.MyBatis的逆向工程
    08.MyBatis整合Log4j
    07.MyBatis中的关联查询
    06.动态SQL和foreach
    05.MyBtais两种取值符号以及输入参数和输出参数
    04.MyBatis别名的设置和类型转换器
    03.Mybatis优化
    02.Mybatis的动态代理方式实现增删改查
    01.MyBatis入门
    网络安全之网络安全(数通安全)0
  • 原文地址:https://www.cnblogs.com/yydcdut/p/4090723.html
Copyright © 2020-2023  润新知