您的位置:首页 > 移动开发 > Android开发

android 学习笔记3-debug调试 数据库 事务 显示界面TextView

2016-12-30 23:11 666 查看
1、调试程序:

    设置断点,点击虫子按钮,我们调试自己的程序一般使用Step Over(F6),学习源码使用Step Into(F5)会进到android源码中

    

    

2、单元测试框架:

    右击工程,新建一个Java Class ,继承android.test.AndroidTestCase

    

    运行,在方法名称上右击选择Run As  --  Android Junit Test

    

        import android.test.AndroidTestCase;

        public class Test extends AndroidTestCase {

            public void test(){

                int result = Tools.damage(1, 2);

                //断言:检测预期和实际结果是否一致

                assertEquals(3, result);

            }

        }

        

    备注:需要在AndroidManifest.xml中定义两个字段

        <instrumentation

            android:name:"android.teset.InstrumentationTestRunner"

            android:targetPackage=""//测试的包名

            ></instrumentation>

        在application字段中添加

        <uses-library android:name="android.test.runner"/>

        

        

3、创建SQLite数据库:使用SQLiteOpenHelper类,数据库查看可以使用工具 SQLite Expert Professional

    我们先创建一个类

    onCreate方法在开始创建的时候调用,以后就不用,onUpgrade在升级数据库的时候用

    

        public class MyOpenHelper extends SQLiteOpenHelper {

            public MyOpenHelper(Context context) {

                //arg1:数据库文件名字

                //arg2:游标工厂,游标等同于结果集,传null使用默认工厂

                //arg3:版本,不能小于1,用于升级

                super(context, "people.db", null, 1);//我们在构造函数里面定死数据库,避免后面修改出错

            }

            //创建数据库时调用

            @Override

            public void onCreate(SQLiteDatabase db) {

                //创建数据库的时候把表也创建了

                db.execSQL("create table person(_id integer primary key autoincrement, name char(10), phone char(20), salary integer(10))");

            }

            //升级数据库时调用

            @Override

            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

                System.out.println("数据库升级");

            }

        }

        
//创建OpenHelper对象
MyOpenHelper oh = new MyOpenHelper(getContext(), "person.db", null, 1);
//获得数据库对象,如果数据库不存在,先创建数据库,后获得,如果存在,则直接获得
SQLiteDatabase db = oh.getWritableDatabase();

    

    备注:

        getWritableDatabase():打开可读写的数据库

        getReadableDatabase():在磁盘空间不足时打开只读数据库,否则打开可读写数据库,和write一样

        

        

        

4、使用SQL语句对数据库的增删改查:

        insert into person (name, phone, money) values ('张三', '159874611', 2000);

        delete from person where name = '李四' and _id = 4;

        update person set money = 6000 where name = '李四';

        select name, phone from person where name = '张三';

        

        //插入
db.execSQL("insert into person (name, phone, money) values (?, ?, ?);", new Object[]{"张三", 1234567890, 1000});
//查找
Cursor cs = db.rawQuery("select _id, name, money from person where name = ?;", new String[]{"张三"});

        

        备注:android习惯用_id表示主键

        

5、使用api实现增删改查

    插入

            //以键值对的形式保存要存入数据库的数据

            ContentValues cv = new ContentValues();

            cv.put("name", "张三");

            cv.put("phone", 1234567890);

            cv.put("money", 1000);

            //返回值是改行的主键,如果出错返回-1

            long i = db.insert("person", null, cv);//这里null一般用不到

    删除

            //返回值是删除的行数

            int i = db.delete("person", "_id = ? and name = ?", new String[]{"1", "张三"});

    修改

        

            ContentValues cv = new ContentValues();

            cv.put("money", 1000);

            int i = db.update("person", cv, "name = ?", new String[]{"张三"});

    查询

            //arg1:要查询的字段

            //arg2:查询条件

            //arg3:填充查询条件的占位符

            Cursor cs = db.query("person", new String[]{"name", "money"}, "name = ?", new String[]{"张三"}, null, null, null);

            while(cs.moveToNext()){

                // 获取指定列的索引值

                String name = cs.getString(cs.getColumnIndex("name"));

                String money = cs.getString(cs.getColumnIndex("money"));

                System.out.println(name + ";" + money);

            } 

            

            

6、事务

    保证多条SQL语句要么同时成功,要么同时失败

    最常见案例:银行转账

    例如:
try {
//开启事务
db.beginTransaction();
...........
//设置事务执行成功
db.setTransactionSuccessful();//如果执行到这条语句了,表示成功了
} finally{
//关闭事务
//如果此时已经设置事务执行成功,则sql语句生效,否则不生效
db.endTransaction();
}   

        

        

7、将查询到的数据显示在界面上:使用TextView

    View对象可以使用 findByViewId函数去找,也可以new的

    

    a、先查询数据,查询出来需要保存到一个地方,(名称,号码,薪水),用数组和集合都不好存,我们需要创建一个类去存

        这种类一般定义在包名+domain这个包下面

    

        public class Person {

            private String name;

            private String phone;

            private int salary;

            public String getName() {

                return name;

            }

            public void setName(String name) {

                this.name = name;

            }

            public String getPhone() {

                return phone;

            }

            public void setPhone(String phone) {

                this.phone = phone;

            }

            public int getSalary() {

                return salary;

            }

            public void setSalary(int salary) {

                this.salary = salary;

            }

            @Override

            public String toString() {

                return "name=" + name + ", phone=" + phone + ", salary="

                        + salary;

            }

            public Person(String name, String phone, int salary) {

                super();

                this.name = name;

                this.phone = phone;

                this.salary = salary;

            }

        }

    

        b、将数据存到对象里面

            List<Person> personList;

            personList = new ArrayList<Person>();

            Cursor cs = db.query("person", null, null, null, null, null, null);

            while(cs.moveToNext()){

                String name = cs.getString(cs.getColumnIndex("name"));

                String phone = cs.getString(cs.getColumnIndex("phone"));

                String money = cs.getString(cs.getColumnIndex("money"));

                //把读到的数据封装至Person对象

                Person p = new Person(name, phone, money);

                //把person对象保存至集合中

                personList.add(p);

            }

            

        c、创建TextView对象

            for (Person p : personList) {

                TextView tv = new TextView(this);//创建文本框

                tv.setText(p.toString());//设置文本框的内容

                tv.setTextSize(12);

            }        

            

        d、这样只是创建了TextView,在内存中,但是界面上还是显示不出来,我们需要将TextView设置为布局LinearLayout的子节点

            LinearLayout ll = (LinearLayout) findViewById(R.id.ll);//将LinearLayout设一个id,找到这个LinearLayout

            ll.addView(tv);//添加子节点

            

            代码优化成这样:

                LinearLayout ll = (LinearLayout) findViewById(R.id.ll);

                for (Person p : personList) {

                    //创建文本框

                    TextView tv = new TextView(this);//或者MainActivity.this也行

                    tv.setText(p.toString());

                    tv.setTextSize(12);

                    //把文本框设置为线性布局的子节点

                    ll.addView(tv);

                }

        这样就可以显示数据,但是现在还不能移动查看没显示出来的数据

        

        e、我们可以在xml文件中使用ScrollView包裹住LinearLayout,实现竖直滑动,水平滑动是HorizontalScrollView

            例如:

            <ScrollView 

                xmlns:android="http://schemas.android.com/apk/res/android"

                xmlns:tools="http://schemas.android.com/tools"

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                >

                

                <LinearLayout 

                    android:id="@+id/ll"

                    android:layout_width="match_parent"

                    android:layout_height="match_parent"

                    tools:context=".MainActivity" 

                    android:orientation="vertical"

                    >

                </LinearLayout>

                

            </ScrollView>

            

        这样我们显示的数据就可以滑动啦

        

        
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: