zoukankan      html  css  js  c++  java
  • 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为列名的列

    我是天王盖地虎的分割线                                                             

  • 相关阅读:
    oracle中Blob和Clob类型的区别
    为什么要分库分表
    Enable file editing in Visual Studio's debug mode
    SQL Server Dead Lock Log
    Debug .NET Framework Source
    SQL Server text field里面有换行符的时候copy到excel数据会散乱
    诊断和修复Web测试记录器(Web Test Recorder)问题
    Can't load Microsoft.ReportViewer.ProcessingObjectModel.dll
    'telnet' is not recognized as an internal or external command
    Linq to XML
  • 原文地址:https://www.cnblogs.com/yydcdut/p/4090723.html
Copyright © 2011-2022 走看看