Android应用开发基础之数据存储和界面展现(三)

时间:2015-01-06 02:02:14   收藏:0   阅读:536

生成XML文件备份短信

使用StringBuffer拼接字符串

使用XMl序列化器生成xml文件


pull解析xml文件

拿到xml文件

    InputStream is = getClassLoader().getResourceAsStream("weather.xml");

拿到pull解析器

    XmlPullParser xp = Xml.newPullParser();

开始解析


测试


单元测试junit


SQLite数据库

创建数据库

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

数据库的增删改查

SQL语句

执行SQL语句实现增删改查

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

使用api实现增删改查

事务


把数据库的数据显示至屏幕

  1. 任意插入一些数据
  2. 定义业务bean:Person.java
  3. 读取数据库的所有数据

    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对象保存至集合中
        people.add(p);
    }
    
  4. 把集合中的数据显示至屏幕

     LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
     for(Person p : people){
         //创建TextView,每条数据用一个文本框显示
         TextView tv = new TextView(this);
         tv.setText(p.toString());
         //把文本框设置为ll的子节点
         ll.addView(tv);
     }
    
  5. 分页查询

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

ListView

BaseAdapter

条目的缓存

原文:http://www.cnblogs.com/McCa/p/4204952.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!