データベースの二種類のCRUDと事務、listview
12646 ワード
1 Androidでデータベース作成
? 。
mysql sqlserver2000 sqlite
SqliteOpenHelper
:
1. SqliteOpenHelper, , oncreate ,onupgrade
:
//context : , name: factory: cursor , null
//version: , 1 , ,onUpgrade ,4.0
super(context, "info.db", null,1);
2. , getReadableDatabase() ,
3. oncreate onupgrdate :
oncreate ; , sql ;SQLiteDatabase db sql
//onUpgrade ;
getWritableDatabase getReadableDatabase SqliteDatabase.
:
getReadableDatabase:
, , 。
getWritableDatabase:
, , 。
2 Androidの下でデータベースの第1種の方式は添削して調べます.1. , getReadableDatabase , SqliteDatebase
2. SqliteDatebase execSql() , rawQuery 。
****** : , sql 。sql ,
private MySqliteOpenHelper mySqliteOpenHelper;
public InfoDao(Context context){
//
mySqliteOpenHelper = new MySqliteOpenHelper(context);
}
public void add(InfoBean bean){
// sql sqliteDatabase
// getReadableDatabase ,
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//sql:sql , bindArgs:sql
db.execSQL("insert into info(name,phone) values(?,?);", new Object[]{bean.name,bean.phone});
//
db.close();
}
public void del(String name){
// sql sqliteDatabase
// getReadableDatabase ,
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//sql:sql , bindArgs:sql
db.execSQL("delete from info where name=?;", new Object[]{name});
//
db.close();
}
public void update(InfoBean bean){
// sql sqliteDatabase
// getReadableDatabase ,
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//sql:sql , bindArgs:sql
db.execSQL("update info set phone=? where name=?;", new Object[]{bean.phone,bean.name});
//
db.close();
}
public void query(String name){
// sql sqliteDatabase
// getReadableDatabase ,
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//sql:sql , selectionArgs: , cursor
Cursor cursor = db.rawQuery("select _id, name,phone from info where name = ?", new String []{name});
// Cursor
if(cursor != null && cursor.getCount() >0){// cursor
// ,
while(cursor.moveToNext()){// ,
//
int id = cursor.getInt(0);
String name_str = cursor.getString(1);
String phone = cursor.getString(2);
System.out.println("_id:"+id+";name:"+name_str+";phone:"+phone);
}
cursor.close();//
}
//
db.close();
}
3 Androidの下でもう一つの添削を行います.1. , getReadableDatabase , SqliteDatebase
2. SqliteDatebase insert,update,delete ,query 。
****** : , sql , , 。 , 。
private MySqliteOpenHelper mySqliteOpenHelper;
public InfoDao(Context context){
//
mySqliteOpenHelper = new MySqliteOpenHelper(context);
}
public boolean add(InfoBean bean){
// sql sqliteDatabase
// getReadableDatabase ,
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
ContentValues values = new ContentValues();// map ,
values.put("name", bean.name);
values.put("phone", bean.phone);
//table: , nullColumnHack: , , values: , : Id ,-1
long result = db.insert("info", null, values);// sql
//
db.close();
if(result != -1){//-1
return true;
}else{
return false;
}
}
public int del(String name){
// sql sqliteDatabase
// getReadableDatabase ,
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//table : , whereClause: , whereArgs: ; :
int result = db.delete("info", "name = ?", new String[]{name});
//
db.close();
return result;
}
public int update(InfoBean bean){
// sql sqliteDatabase
// getReadableDatabase ,
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
ContentValues values = new ContentValues();// map ,
values.put("phone", bean.phone);
//table: , values: , whereClause: , whereArgs: , :
int result = db.update("info", values, "name = ?", new String[]{bean.name});
//
db.close();
return result;
}
public void query(String name){
// sql sqliteDatabase
// getReadableDatabase ,
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//table: , columns: , null ; selection: , selectionArgs: ,
//groupBy: , having: , orderBy:
Cursor cursor = db.query("info", new String[]{"_id","name","phone"}, "name = ?", new String[]{name}, null, null, "_id desc");
// Cursor
if(cursor != null && cursor.getCount() >0){// cursor
// ,
while(cursor.moveToNext()){// ,
//
int id = cursor.getInt(0);
String name_str = cursor.getString(1);
String phone = cursor.getString(2);
System.out.println("_id:"+id+";name:"+name_str+";phone:"+phone);
}
cursor.close();//
}
//
db.close();
}
4データベースのトランザクション : sql , , , ,
//
public void transtation(View v){
//1.
BankOpenHelper bankOpenHelper = new BankOpenHelper(this);
//2. getReadableDatabase , , SqliteDatabase (sql )
SQLiteDatabase db = bankOpenHelper.getReadableDatabase();
//3. , 200, 200
db.beginTransaction();//
try {
db.execSQL("update account set money= money-200 where name=?",new String[]{" "});
int i = 100/0;//
db.execSQL("update account set money= money+200 where name=?",new String[]{" "});
db.setTransactionSuccessful();// sql
} finally {
db.endTransaction();// , , sql
}
}
5 listview入門 ListView , , ListAdapter 。EditText Button TextView ImageView Checkbox 。
1. Listview
2. listview
3. Adapter BaseAdapter, 4 , getcount,getview
getcount: listview
getview: listview 。
4. Adapter , listview。
listview.setAdapter(ListAdapter adapter);
6 listview最適化adapter getview convertView,convertView view , , , , getview 。
TextView view = null;
if(convertView != null){// converView ,
view = (TextView) convertView;
}else{
view = new TextView(mContext);// textView
}
return view;
7 listview—タイガーマシンjavaweb mvc
m....mode....javabean
v....view....jsp
c....control...servlet
listview mvc
m....mode....Bean
v....view....listview
c....control...adapter
8 listview表示原理1. listview getcount
2. listview getview
3. item ,
4. listview , , 。
9複雑なlistview画面表示1. listview
2. listview
3. list ( ), adapter , adapter???
4. adapter BaseAdapter, 4
getcount: , 。
getView: ; 。 ?????
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
//1. converView listview, view getview
if(convertView != null){
view = convertView;
}else {
//context: , resource: view layout id, root: layout root(ViewGroup) getview , null
view = View.inflate(context, R.layout.item_news_layout, null);// view
}
//2. view
ImageView item_img_icon = (ImageView) view.findViewById(R.id.item_img_icon);
TextView item_tv_des = (TextView) view.findViewById(R.id.item_tv_des);
TextView item_tv_title = (TextView) view.findViewById(R.id.item_tv_title);
//3. postion list ,Bean
NewsBean newsBean = list.get(position);
//4.
item_img_icon.setImageDrawable(newsBean.icon);// imageView
item_tv_title.setText(newsBean.title);
item_tv_des.setText(newsBean.des);
return view;
}
5. adapter listview
6. listview , , 。 ?????????
// listview
lv_news.setOnItemClickListener(this);
//listview parent: listviw view: view position: id: id
public void onItemClick(AdapterView> parent, View view, int position,
long id) {
// bean url
NewsBean bean = (NewsBean) parent.getItemAtPosition(position);
String url = bean.news_url;
//
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
1. listview
2. listview
3. list , adapter 。
4. Adapter BaseAdatper, list ,
a.
b. getCount
c.getView :
1. convertview, , , view getview 。
view = View.inflater(Context context, int resuorceId,ViewGroup root)
2. view , list bean
3. list postion Bean
4. bean
d.getItem : list postion bean
e.getItemId, postion
5. Adapter , listview
6. listview
listview.setOnItem....
7. OnItemClicklistener , bean , url, Intent ;
10は常にinflateの書き方を取得します. 1.
//context: , resource: view layout id, root: layout root(ViewGroup) codify , null
//view = View.inflate(context, R.layout.item_news_layout, null);// view
2.
// LayoutInflater view
//view = LayoutInflater.from(context).inflate(R.layout.item_news_layout, null);
3.
// context LayoutInflater, LayoutInflater view
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.item_news_layout, null);
11 arrayadapter //
ListView lv_array = (ListView) findViewById(R.id.lv_array);
ListView lv_simple = (ListView) findViewById(R.id.lv_simple);
// arrayAdapter
//context , resource: id, textViewResourceId: textview id, objects: texitview
ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.item_listview_layout, R.id.item_tv_class, classz);
lv_array.setAdapter(arrayAdapter);
12 simpleadapper // simpleAdapter, simpleAdapter
ArrayList