附录A 部分代码.docx
《附录A 部分代码.docx》由会员分享,可在线阅读,更多相关《附录A 部分代码.docx(9页珍藏版)》请在冰豆网上搜索。
附录A部分代码packagecom.dao;
importjava.util.ArrayList;
importjava.util.List;
importandroid.content.ContentValues;
importandroid.content.Context;
importandroid.database.Cursor;
importandroid.database.sqlite.SQLiteDatabase;
importandroid.database.sqlite.SQLiteDatabase.CursorFactory;
importandroid.database.sqlite.SQLiteOpenHelper;
publicclassContactDaoextendsSQLiteOpenHelper{
privateSQLiteDatabasedb;
publicContactDao(Contextcontext,Stringname,CursorFactoryfactory,intversion){
super(context,name,factory,version);
}
@Override
publicvoidonCreate(SQLiteDatabasedb){
Stringsql="createtablet_contact(c_idintegerprimarykey
autoincrement,g_name,c_name,c_birthday,c_phone,c_e_mail,c_address)";
db.execSQL(sql);
}
@Override
publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){
}
publicListgetContactList(){
Listlist=newArrayList();
db=getReadableDatabase();
Cursorc=db.query("t_contact",newString[]{"c_id","g_name","c_name"},null,null,null,null,
null);
//cursor是什么意思有何作用
while(c.moveToNext()){
Strings=c.getString(c.getColumnIndex("g_name"));
inti=s.length();
if(i>=5){
s=s.substring(0,2)+"......";华图毕业设计网提供QQ:
8694378222}
Stringstr=c.getString(c.getColumnIndex("c_id"))+"."+s+"姓名
"+c.getString(c.getColumnIndex("c_name"));
list.add(str);
}
db.close();
returnlist;
}
publicintdeleteContact(Stringid){
db=getWritableDatabase();
inti=db.delete("t_contact","c_id=?
",newString[]{id});
db.close();
returni;
}
publicListgetContactList(Stringname,inti){
Listlist=newArrayList();
db=getReadableDatabase();
Cursorc=null;
switch(i){
case1:
c=db.query("t_contact",newString[]{"c_id","g_name","c_name"},"c_name=?
",new
String[]{name},null,null,null);
break;//string数组何用
case2:
c=db.query("t_contact",newString[]{"c_id","g_name","c_name"},"g_name=?
",new
String[]{name},null,null,null);
break;
}
while(c.moveToNext()){
Strings=c.getString(c.getColumnIndex("g_name"));
intii=s.length();
if(ii>=5){
s=s.substring(0,2)+"......";
}
Stringstr=c.getString(c.getColumnIndex("c_id"))+"."+s+"姓名
"+c.getString(c.getColumnIndex("c_name"));
list.add(str);
}
c.close();
db.close();华图毕业设计网提供QQ:
8694378223returnlist;
}
publiclongaddContact(Stringg_name,Stringc_name,Stringc_birthday,Stringc_phone,String
c_e_mail,Stringc_address){
db=getWritableDatabase();
ContentValuesvalues=newContentValues();
values.put("g_name",g_name);
values.put("c_name",c_name);
values.put("c_birthday",c_birthday);
values.put("c_phone",c_phone);
values.put("c_e_mail",c_e_mail);
values.put("c_address",c_address);
longl=db.insert("t_contact",null,values);
db.close();
returnl;
}
publicString[]getContact(Stringc_id){
String[]str=newString[6];
db=getReadableDatabase();
Cursorcursor=db.query("t_contact",new
String[]{"g_name","c_name","c_birthday","c_phone","c_e_mail","c_address"},"c_id=?
",newString[]{c_id},
null,null,null);
if(cursor.moveToNext()){
str[0]=cursor.getString(cursor.getColumnIndex("g_name"));
str[1]=cursor.getString(cursor.getColumnIndex("c_name"));
str[2]=cursor.getString(cursor.getColumnIndex("c_birthday"));
str[3]=cursor.getString(cursor.getColumnIndex("c_phone"));
str[4]=cursor.getString(cursor.getColumnIndex("c_e_mail"));
str[5]=cursor.getString(cursor.getColumnIndex("c_address"));
}
db.close();
returnstr;
}
publicintupdateContact(Stringc_id,Stringg_name,Stringc_name,Stringc_birthday,String
c_phone,Stringc_e_mail,Stringc_address){
db=getWritableDatabase();
ContentValuesvalues=newContentValues();
values.put("g_name",g_name);
values.put("c_name",c_name);
values.put("c_birthday",c_birthday);
values.put("c_phone",c_phone);
values.put("c_e_mail",c_e_mail);华图毕业设计网提供QQ:
8694378224values.put("c_address",c_address);
inti=db.update("t_contact",values,"c_id=?
",newString[]{c_id});
db.close();
returni;
}
}packagecom.dao;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
importandroid.content.Context;
importandroid.database.Cursor;
importandroid.database.sqlite.SQLiteDatabase;
importandroid.database.sqlite.SQLiteDatabase.CursorFactory;
importandroid.database.sqlite.SQLiteOpenHelper;
publicclassCreateNoteexten