ym——Android从零开始(8)(Sqlite事务+结合ListView显示+include标签)(新)

转载请注明本文出自CYM的博客(http://blog.csdn.net/cym492224103),谢谢支持!


前言

上篇对Sqlite有了一个基本的了解以及使用,接下我们来学习一下对数据操作的优化,和之前学习过的ListView的结合使用吧~!


SqliteDatabase事务

SQLiteDatabase控制事务方法:

的BeginTransaction();        开启事务

setTransactionSuccessful(); 设置事务成功

endTransaction()方法;结束事务

使用事务提交可以提高CRUD操作的速度

<p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">   try {</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                db.execSQL("update student set amount=amount-300 where name=?", new Object[]{"<span style="font-family:宋体;word-wrap: break-word;">张三</span><span style="font-family:Times New Roman;word-wrap: break-word;">"});</span></p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                db.execSQL("update student set amount=amount+300 where name=?", new Object[]{"<span style="font-family:宋体;word-wrap: break-word;">李四</span><span style="font-family:Times New Roman;word-wrap: break-word;">"});</span></p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                db.setTransactionSuccessful();         //<span style="font-family:宋体;word-wrap: break-word;">设置事务成功</span></p><br style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;" /><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        } catch (Exception e) {</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                System.out.println("<span style="font-family:宋体;word-wrap: break-word;">事务执行出现异常,不能设置事务成功!</span><span style="font-family:Times New Roman;word-wrap: break-word;">");</span></p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        }finally{</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        //<span style="font-family:宋体;word-wrap: break-word;">无论是否出现异常,都要结束事务。</span></p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                db.endTransaction();</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        }</p>


CMD常见指令:

sqlite3的xx.db查看XX数据库

.tables查看所有表

.exit退出

.databases查看所有数据库


包括标签:

标签实现组件复用

将另一个布局文件加载到当前布局文件,实现组件复用

<p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;"><!-- <span style="font-family:宋体;word-wrap: break-word;">加载一个布局文件 </span><span style="font-family:Times New Roman;word-wrap: break-word;">--></span></p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;"><include layout="@layout/item"/></p>


属性机器人:知名度=有三个选项

<! -

可见:可见

           都是不可见

    无形的:  还占据空间

    走了:不占据空间

     - >


列表显示多条数据

使用我们之前学过的simpleAdapter

代码:

<p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">OtherFishService ofs = new OtherFishService(this);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">            List<Fish> fishs = ofs.query();</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">            List<Map<String,Object>>data=new ArrayList<Map<String,Object>>();</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">            for(Fish f:fishs){</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                    Map<String,Object> map = new HashMap<String, Object>();</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                    map.put("_id", f._id);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                    map.put("name", f.name);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                    map.put("age", f.age);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                    data.add(map);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">            }</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">            String[] from = new String[]{"_id","name","age"};</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">            int[] to = new int[]{R.id.tv_id,R.id.tv_name,R.id.tv_age};</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">            SimpleAdapter adapter = new SimpleAdapter(this,</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                            data,</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                            R.layout.item,</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                            from,</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                            to);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">            lv.setAdapter(adapter);</p>

以上可以实现但是过于麻烦!

我们还可以使用专门为数据库提供的

的CursorAdapter

<p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">OtherFishService ofs = new OtherFishService(this);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">            Cursor c = ofs.getAllCursor();</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">            String[] from = new String[]{"_id","name","age"};</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">            int[] to = new int[]{R.id.tv_id,R.id.tv_name,R.id.tv_age};</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">            SimpleCursorAdapter adapter = new SimpleCursorAdapter(</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                            this,</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                            R.layout.item,</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                            c,</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                            from,</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                            to);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">            lv.setAdapter(adapter);</p>

我们不单单只用android给我们提过的CursorAdapter我们还可以自定义一个CursorAdapter

自定义的CursorAdapter

<p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">public class MyCursorAdapter extends CursorAdapter {</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        //<span style="font-family:宋体;word-wrap: break-word;">布局加载器(是一个服务,用来加载</span><span style="font-family:Times New Roman;word-wrap: break-word;">xml</span><span style="font-family:宋体;word-wrap: break-word;">布局文件到</span><span style="font-family:Times New Roman;word-wrap: break-word;">java</span><span style="font-family:宋体;word-wrap: break-word;">代码中)</span></p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        private LayoutInflater mInflater;</p><br style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;" /><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        public MyCursorAdapter(Context context, Cursor c) {</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                super(context, c);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        mInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);</p><br style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;" /><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                mInflater = LayoutInflater.from(context);</p><br style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;" /><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        }</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        //<span style="font-family:宋体;word-wrap: break-word;">创建一个</span><span style="font-family:Times New Roman;word-wrap: break-word;">item</span><span style="font-family:宋体;word-wrap: break-word;">的布局</span></p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        @Override</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        public View newView(Context context, Cursor cursor, ViewGroup parent) {</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                // TODO Auto-generated method stub</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                //<span style="font-family:宋体;word-wrap: break-word;">通过布局加载器加载布局</span></p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                View view = mInflater.inflate(R.layout.item, null);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                return view;</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        }</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        //<span style="font-family:宋体;word-wrap: break-word;">把数据绑定给</span><span style="font-family:Times New Roman;word-wrap: break-word;">item</span><span style="font-family:宋体;word-wrap: break-word;">里面的布局</span></p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        @Override</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        public void bindView(View view, Context context, Cursor cursor) {</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                // TODO Auto-generated method stub</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        //1 <span style="font-family:宋体;word-wrap: break-word;">先得到控件</span></p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                //2 <span style="font-family:宋体;word-wrap: break-word;">得到数据</span></p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                //3 <span style="font-family:宋体;word-wrap: break-word;">绑定数据给控件</span></p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                TextView tv_id = (TextView) view.findViewById(R.id.tv_id);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                TextView tv_name = (TextView) view.findViewById(R.id.tv_name);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                TextView tv_age = (TextView) view.findViewById(R.id.tv_age);</p><br style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;" /><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                String _id = cursor.getString(0);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                String name = cursor.getString(1);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                String age = cursor.getString(2);</p><br style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;" /><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                tv_id.setText(_id);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                tv_name.setText(name);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                tv_age.setText(age);</p><br style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;" /><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                int position = cursor.getPosition();</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                if(position%2 ==0){</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        view.setBackgroundColor(Color.RED);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                }else{</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        view.setBackgroundColor(Color.GREEN);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                }</p><br style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;" /><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        }</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">}</p>

为了实现我们想要的并且系统没提供的效果所以我们要自定义适配器

不仅可以继承的CursorAdapter实现自定义的CursorAdapter,我们还可以通

过继承BaseAdapter。

<p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">private class MyCursorAdapter extends BaseAdapter {</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                private Context context;</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                private Cursor c;</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                private LayoutInflater mInflater;</p><br style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;" /><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                public MycursorAdapter1(Context context, Cursor c) {</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        super();</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        this.context = context;</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        this.c = c;</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                }</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                @Override</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                public int getCount() {</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        // TODO Auto-generated method stub</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        return c.getCount();</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                }</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                @Override</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                public Object getItem(int position) {</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        Person p = null;</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        if (c.moveToPosition(position)) {</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                                String id = c.getString(0);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                                String name = c.getString(1);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                                String age = c.getString(2);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                                p = new Person(id, name, age);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        }</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        return p;</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                }</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                @Override</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                public long getItemId(int position) {</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        // TODO Auto-generated method stub</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        return position;</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                }</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                @Override</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                public View getView(int position, View convertView, ViewGroup parent) {</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        //1<span style="font-family:宋体;word-wrap: break-word;">得到视图</span></p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">//2<span style="font-family:宋体;word-wrap: break-word;">从视图中拿到要显示对象</span></p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">//3<span style="font-family:宋体;word-wrap: break-word;">从</span><span style="font-family:Courier New;word-wrap: break-word;">cursor</span><span style="font-family:宋体;word-wrap: break-word;">里面拿值</span></p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">//4<span style="font-family:宋体;word-wrap: break-word;">绑定</span></p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">//5<span style="font-family:宋体;word-wrap: break-word;">返回</span></p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">注意:getView方法实际会执行原本次数2<span style="font-family:宋体;word-wrap: break-word;">倍(例:</span><span style="font-family:Courier New;word-wrap: break-word;">7</span><span style="font-family:宋体;word-wrap: break-word;">条数据与,</span>getView执行7*2<span style="font-family:宋体;word-wrap: break-word;">次),第一遍执行是为了测试有多少条数据,第</span><span style="font-family:Courier New;word-wrap: break-word;">2</span><span style="font-family:宋体;word-wrap: break-word;">遍执行才是真正的现实操作</span></p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        View view = mInflater.inflate(R.layout.item, null);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        TextView tv_id = (TextView) view.findViewById(R.id.bt_id);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        TextView tv_name = (TextView) view.findViewById(R.id.bt_name);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        TextView tv_age = (TextView) view.findViewById(R.id.bt_age);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        if (c.moveToPosition(position)) {</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                                String id = c.getString(0);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                                String name = c.getString(1);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                                String age = c.getString(2);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                                tv_id.setText(id);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                                tv_name.setText(name);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                                tv_age.setText(age);</p><br style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;" /><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        }</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        if ( position% 2 == 0) {</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                                view.setBackgroundColor(Color.RED);</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        }</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        return view;</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                }</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        }</p>

当我们用完光标的时候,怎么关闭他?

<p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">//应用回收资源  退出程序时调用该方法</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        @Override</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        protected void onDestroy() {</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                // TODO Auto-generated method stub</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                super.onDestroy();r</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                Cursor c = adapter.getCursor();</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                if(c != null && !c.isClosed()){</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                        c.close();</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">                }</p><p align="left" style="word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;">        }</p>

你可能感兴趣的:(ym——Android从零开始(8)(Sqlite事务+结合ListView显示+include标签)(新))