android高效编程之使用本地变量

本文介绍了一种在Android开发中优化代码访问效率的方法:通过将类成员变量赋值给局部变量来减少对成员变量的访问次数,从而提高程序运行效率。这种方法在Dalvik虚拟机上尤为有效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

hava a look at the following code you will find that

 

 We assign the mNotesCursor field to a local variable at the start of the method. This is done as an optimization of the Android code. Accessing a local variable is much more efficient than accessing a field in the Dalvik VM, so by doing this we make only one access to the field, and five accesses to the local variable, making the routine much more efficient. It is recommended that you use this optimization when possible

 

 

   @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
       
        Cursor c = mNotesCursor;//We assign the mNotesCursor field to a local variable
        c.moveToPosition(position);
        Intent i = new Intent(this,NoteEdit.class);
        i.putExtra(NotesDbAdapter.KEY_ROWID, id);
        i.putExtra(NotesDbAdapter.KEY_TITLE,
                c.getString(c.getColumnIndex(NotesDbAdapter.KEY_TITLE)));
        i.putExtra(NotesDbAdapter.KEY_BODY,
                c.getString(c.getColumnIndex(NotesDbAdapter.KEY_BODY)));
        startActivityForResult(i, ACTIVITY_EDIT);
       
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值