Android学习第3天,test、sqlite、listview、dialog

来源:互联网 发布:百度地图编辑软件 编辑:程序博客网 时间:2024/06/05 09:06

测试

按岗位划分

  • 黑盒测试:测试逻辑业务
  • 白盒测试:测试逻辑方法

按测试粒度分

  • 方法测试:function test
  • 单元测试:unit test
  • 集成测试:integration test
  • 系统测试:system test

按测试的暴力程度分

  • 冒烟测试:smoke test
  • 压力测试:pressure test

单元测试

  • junit
  • 在清单文件中指定指令集
//指定该测试框架要测试哪一个项目<instrumentation     android:name="android.test.InstrumentationTestRunner"android:targetPackage="com.itheima.junit"></instrumentation>
  • 定义使用的类库
<uses-library android:name="android.test.runner"/>

SQLite运用

  • 数据库创建
public class MyOpenHelper extends SQLiteOpenHelper {    public MyOpenHelper(Context context, String name, CursorFactory factory,            int version) {        super(context, name, factory, version);    }    // 创建    @Override    public void onCreate(SQLiteDatabase db) {        db.execSQL("create table person(_id integer primary key autoincrement, name char(10), salary char(20), phone integer(20))");    }    // 升级    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {        System.out.println("onUpgrade");    }}
  • 数据库操作
public class TestCase extends AndroidTestCase {    private MyOpenHelper oh;    private SQLiteDatabase db;    public void test(){        MyOpenHelper oh = new MyOpenHelper(getContext(), "people.db", null, 1);        SQLiteDatabase db = oh.getWritableDatabase();    }    // 这个对象初始化完成后,调用测试函数前执行    @Override    protected void setUp() throws Exception {        super.setUp();        oh = new MyOpenHelper(getContext(), "people.db", null, 1);        db = oh.getWritableDatabase();    }    // 测试函数调用完成后执行    @Override    protected void tearDown() throws Exception {        super.tearDown();        db.close();    }    public void insert(){        db.execSQL("insert into person (name, salary, phone)values(?, ?, ?)", new Object[]{"a", 14000, "13888"});    }    public void delete(){        db.execSQL("delete from person where name = ?", new Object[]{"a"});    }    public void update(){        db.execSQL("update person set phone = ? where name = ?", new Object[]{186666, "a"});    }    public void select(){        Cursor cursor = db.rawQuery("select name, salary from person", null);        while(cursor.moveToNext()){            String name = cursor.getString(cursor.getColumnIndex("name"));            String salary = cursor.getString(1);            System.out.println(name + ";" + salary);        }    }    // 使用系统提供的api操作    public void insertApi(){        ContentValues values = new ContentValues();        values.put("name", "a");        values.put("phone", "15999");        values.put("salary", 16000);        db.insert("person", null, values);    }    public void deleteApi(){        int i = db.delete("person", "name = ? and _id = ?", new String[]{"a", "3"});        System.out.println(i);    }    public void updateApi(){        ContentValues values = new ContentValues();        values.put("salary", 26000);        int i = db.update("person", values, "name = ?", new String[]{"a"});        System.out.println(i);    }    public void selectApi(){        Cursor cursor = db.query("person", null, null, null, null, null, null, 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 transaction(){        try{            // 开始传输            db.beginTransaction();            ContentValues values = new ContentValues();            values.put("salary", 12000);            db.update("person", values, "name = ?", new String[]{"a"});            values.clear();            values.put("salary", 16000);            db.update("person", values, "name = ?", new String[]{"b"});            int i = 3/0;            // 设置事务成功,如果这条语句没有执行,则本次事务视为不成功            db.setTransactionSuccessful();        }        finally{            // 若事务成功则正常结束,否则会回滚            db.endTransaction();        }    }}

使用ListView显示数据

  • 使用布局文件填充listview
class MyAdapter extends BaseAdapter {    // ListView一共有多少行    @Override    public int getCount() {        return personList.size();    }    // 获取每行view该显示什么内容    @Override    public View getView(int position, View convertView, ViewGroup parent) {        Person p = personList.get(position);                View v = null;        // 查看是否有缓存        if(convertView == null){            v = View.inflate(MainActivity.this, R.layout.item_listview, null);        }        else{            v = convertView;        }        // 获取布局文件的第二种方法//          LayoutInflater inflater = LayoutInflater.from(MainActivity.this);//          View v2 = inflater.inflate(R.layout.item_listview, null);        // 获取布局文件的第三种方法//          LayoutInflater inflater2 = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);//          View v3 = inflater2.inflate(R.layout.item_listview, null);        // 通过布局文件查找TextView        TextView tv_name = (TextView) v.findViewById(R.id.tv_name);        tv_name.setText(p.getName());        TextView tv_phone = (TextView) v.findViewById(R.id.tv_phone);        tv_phone.setText(p.getPhone());        TextView tv_salary = (TextView) v.findViewById(R.id.tv_salary);        tv_salary.setText(p.getSalary());        return v;    }    @Override    public Object getItem(int position) {        return null;    }    @Override    public long getItemId(int position) {        return 0;    }}   
  • 使用其他的adapter
protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        String[] objects = new String[]{                "a",                "b",                "c"        };        ListView lv = (ListView) findViewById(R.id.lv);        // 使用ArrayAdapter//      lv.setAdapter(new ArrayAdapter<String>(this, R.layout.item_listview, R.id.tv_name, objects));        // 使用SimpleAdapter        List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();        Map<String, Object> map1 = new HashMap<String, Object>();        map1.put("photo", R.drawable.photo1);        map1.put("name", "c");        data.add(map1);        Map<String, Object> map2 = new HashMap<String, Object>();        map2.put("photo", R.drawable.photo2);        map2.put("name", "a");        data.add(map2);        Map<String, Object> map3 = new HashMap<String, Object>();        map3.put("photo", R.drawable.photo3);        map3.put("name", "b");        data.add(map3);        lv.setAdapter(new SimpleAdapter(this, data, R.layout.item_listview,                 new String[]{"photo", "name"}, new int[]{R.id.iv_photo, R.id.tv_name}));    }

弹出对话框

// 确定取消对话框public void click1(View v){    AlertDialog.Builder builder = new Builder(this);    builder.setIcon(android.R.drawable.alert_dark_frame);    builder.setTitle("title");    builder.setMessage("内容");    builder.setPositiveButton("确定", new OnClickListener() {        @Override        public void onClick(DialogInterface dialog, int which) {            Toast.makeText(MainActivity.this, "点击了确定按钮", 0).show();        }    });    builder.setNegativeButton("取消", new OnClickListener() {        @Override        public void onClick(DialogInterface dialog, int which) {            Toast.makeText(MainActivity.this, "点击了取消按钮", 0).show();        }    });    // 显示这个对话框    AlertDialog ad = builder.create();    ad.show();}// 单选对话框public void click2(View v){    AlertDialog.Builder builder = new Builder(this);    builder.setTitle("title");    final String[] items = new String[]{            "男",            "女"    };    builder.setSingleChoiceItems(items, -1, new OnClickListener() {        @Override        public void onClick(DialogInterface dialog, int which) {            Toast.makeText(MainActivity.this, "你选择了:" + items[which], 0).show();            dialog.dismiss();        }    });    builder.show();}// 多选对话框public void click3(View v){    AlertDialog.Builder builder = new Builder(this);    builder.setTitle("title");    final String[] items = new String[]{            "名字1",            "名字2",            "名字3",            "名字4"    };    final boolean[] checkedItems = new boolean[]{            true,            true,            false,            false    };    builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {        @Override        public void onClick(DialogInterface dialog, int which, boolean isChecked) {            checkedItems[which] = isChecked;        }    });    builder.setPositiveButton("确定", new OnClickListener() {        @Override        public void onClick(DialogInterface dialog, int which) {            String text = "";            for(int i = 0; i < 4; i++){                text += checkedItems[i]? items[i] + "," : "";            }            Toast.makeText(MainActivity.this, text, 0).show();            dialog.dismiss();        }    });    builder.show();}
0 0