InputStream is = getClassLoader().getResourceAsStream("weather.xml");
XmlPullParser xp = Xml.newPullParser();
拿到指针所在当前节点的事件类型
int type = xp.getEventType();
事件类型主要有五种
如果获取到的事件类型不是END_DOCUMENT,就说明解析还没有完成,如果是,解析完成,while循环结束
while(type != XmlPullParser.END_DOCUMENT)
当我们解析到不同节点时,需要进行不同的操作,所以判断一下当前节点的name
当解析到name开始节点时,获取下一个节点的文本内容,temp、pm也是一样
case XmlPullParser.START_TAG:
//获取当前节点的名字
if("weather".equals(xp.getName())){
citys = new ArrayList<City>();
}
else if("city".equals(xp.getName())){
city = new City();
}
else if("name".equals(xp.getName())){
//获取当前节点的下一个节点的文本
String name = xp.nextText();
city.setName(name);
}
else if("temp".equals(xp.getName())){
String temp = xp.nextText();
city.setTemp(temp);
}
else if("pm".equals(xp.getName())){
String pm = xp.nextText();
city.setPm(pm);
}
break;
当解析到city的结束节点时,说明city的三个子节点已经全部解析完了,把city对象添加至list
case XmlPullParser.END_TAG:
if("city".equals(xp.getName())){
citys.add(city);
}
白盒测试
根据测试粒度
根据测试暴力程度
定义一个类继承AndroidTestCase,在类中定义方法,即可测试该方法
在指定指令集时,targetPackage指定你要测试的应用的包名
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.itheima.junit"
></instrumentation>
定义使用的类库
<uses-library android:name="android.test.runner"></uses-library>
断言的作用,检测运行结果和预期是否一致
创建数据库需要使用的api:SQLiteOpenHelper
必须定义一个构造方法:
//arg1:数据库文件的名字
//arg2:游标工厂
//arg3:数据库版本
public MyOpenHelper(Context context, String name, CursorFactory factory, int version){
}
//创建OpenHelper对象
MyOpenHelper oh = new MyOpenHelper(getContext(), "person.db", null, 1);
//获得数据库对象,如果数据库不存在,先创建数据库,后获得,如果存在,则直接获得
SQLiteDatabase db = oh.getWritableDatabase();
在创建数据库时创建表
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table person (_id integer primary key autoincrement, name char(10), phone char(20), money integer(20))");
}
OpenHelper类:
构造函数写死是为了反之更改时出错
public class MyOpenHelper extends SQLiteOpenHelper {
public MyOpenHelper(Context context) {
super(context, "person.db", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
System.out.println("数据库升级");
}
}
代码:
public class Test extends AndroidTestCase{
private MyOpenHelper oh;
private SQLiteDatabase db;
public void test(){
SQLiteDatabase db = oh.getWritableDatabase();
}
@Override
protected void setUp() throws Exception {
// TODO Auto-generated method stub
super.setUp();
oh = new MyOpenHelper(getContext());
db = oh.getWritableDatabase();
}
public void insert(){
db.execSQL("insert into person(name, phone, salary) values(?,?,?)",new Object[]{"guan1", "121212",13333});
db.execSQL("insert into person(name, phone, salary) values(?,?,?)",new Object[]{"hang1", "131212",13333});
db.execSQL("insert into person(name, phone, salary) values(?,?,?)",new Object[]{"yes1", "141212",13333});
}
public void delete(){
db.execSQL("delete from person where name = ?", new Object[]{"yes"});
}
public void select(){
Cursor cursor = db.rawQuery("select * from person", null);
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
String salary = cursor.getString(cursor.getColumnIndex("salary"));
System.out.println(name + ":" + phone + "," + salary);
}
}
public void update(){
db.execSQL("update person set salary=? where name = ?",new Object[]{22121,"guan"});
}
@Override
protected void tearDown() throws Exception {
// TODO Auto-generated method stub
super.tearDown();
db.close();
}
}
//以键值对的形式保存要存入数据库的数据
ContentValues cv = new ContentValues();
cv.put("name", "刘能");
cv.put("phone", 1651646);
cv.put("money", 3500);
//返回值是改行的主键,如果出错返回-1
long i = db.insert("person", null, cv);
//返回值是删除的行数
int i = db.delete("person", "_id = ? and name = ?", new String[]{"1", "张三"});
ContentValues cv = new ContentValues();
cv.put("money", 25000);
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);
}
分页查询
Cursor cs = db.query("person", null, null, null, null, null, null, "0, 10");
try {
//开启事务
db.beginTransaction();
...........
//设置事务执行成功
db.setTransactionSuccessful();
} finally{
//关闭事务
//如果此时已经设置事务执行成功,则sql语句生效,否则不生效
db.endTransaction();
}
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);
}
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);
}
主页面的写法:
<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 xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/lt" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
</LinearLayout>
</ScrollView>
必须实现的两个方法
//系统调用此方法,用来获知模型层有多少条数据
@Override
public int getCount() {
return people.size();
}
* 第二个
//系统调用此方法,获取要显示至ListView的View对象
//position:是return的View对象所对应的数据在集合中的位置
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("getView方法调用" + position);
TextView tv = new TextView(MainActivity.this);
//拿到集合中的元素
Person p = people.get(position);
tv.setText(p.toString());
//把TextView的对象返回出去,它会变成ListView的条目
return tv;
}
*布局文件填充为View对象
几个注意事项
不同布局文件的资源文件id可以相同
找id是要调用特定layout的findid方法,不写是找activit_main里面的
TextView如果传入的是int型,会被当成资源id
代码:
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Person p = personList.get(position);
// TextView tv = new TextView(MainActivity.this);
// tv.setText(p.toString());
View view = View.inflate(MainActivity.this, R.layout.item_listview, null);
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
tv_name.setText(p.getName());
TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone);
tv_phone.setText(p.getPhone());
TextView tv_salary = (TextView) view.findViewById(R.id.tv_salary);
tv_salary.setText(p.getSalary()+"");
return view;
}
View view = View.inflate(MainActivity.this, R.layout.item_listview, null);//推荐写法
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
String[] objects = new String[]{
"张三",
"李四",
"王五"
};
ListView lv = (ListView) findViewById(R.id.lv);
//arg1:指定要填充的布局文件
//arg2:指定文本显示至哪一个文本框内
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.item_array, R.id.tv_name, objects));
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
//张三的头像和名字是两种类型的数据,先封装至Map
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("name", "张三");
map1.put("image", R.drawable.photo1);
//把Map封装至List
data.add(map1);
lv.setAdapter(new SimpleAdapter(this, data, R.layout.item_array,
new String[]{"name", "image"}, new int[]{R.id.tv_name, R.id.iv_photo}));