Androidアプリケーション開発-データストレージとインタフェースの展示(二)
8346 ワード
テスト
持ち場別に分ける
テスト粒度別
テストの暴力の程度によって分けます
ユニットテストjunit(Eclipse用)
<instrumentation
android:name="android.test.InstrumentationTestRunner"
//
android:targetPackage="com.itheima.junit"
></instrumentation>
<uses-library android:name="android.test.runner"/>
SQLiteデータベース
// 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))");
}
データベースの削除
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[]{" "});
protected void setUp() throws Exception {
super.setUp();
//
oh = new MyOpenHelper(getContext(), "people.db", null, 1);
}
apiを使用して削除・変更を実現
//
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);
}
取引
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);
}
Cursor cs = db.query("person", null, null, null, null, null, null, "0, 10");
ListView
BaseAdapter
// ,
public int getCount() {
return people.size();
}
// , ListView View
//position: return View
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;
}
エントリのキャッシュ
ダイアログ
OKキャンセルダイアログ
AlertDialog.Builder builder = new Builder(this);
builder.setTitle(" ");
builder.setMessage(" , ");
builder.setPositiveButton(" ", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, " , ", 0).show();
}
});
builder.setNegativeButton(" ", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, " , ", 0).show();
}
});
AlertDialog ad = builder.create();
ad.show();
ラジオダイアログ
AlertDialog.Builder builder = new Builder(this);
builder.setTitle(" ");
final String[] items = new String[]{
" ",
" "
};
//-1
//
builder.setSingleChoiceItems(items, -1, new OnClickListener() {
//dialog:
//which:
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, " " + items[which], 0).show();
//
dialog.dismiss();
}
});
//
builder.show();
複数選択ダイアログ
AlertDialog.Builder builder = new Builder(this);
builder.setTitle(" ");
final String[] items = new String[]{
" ",
" ",
" ",
" "
};
//
final boolean[] checkedItems = new boolean[]{
true,
false,
false,
false,
};
builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {
//which:
//isChecked:
@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) {
StringBuffer sb = new StringBuffer();
for(int i = 0;i < items.length; i++){
sb.append(checkedItems[i] ? items[i] + " " : "");
}
Toast.makeText(MainActivity.this, sb.toString(), 0).show();
}
});
builder.show();