android sqlite通讯录,做了一个通讯录用的SQlite数据库,当我把SQlite数据库里联系人顺序重新排列后,为啥在我的app里显示的还是以前联系人的顺序【android开发吧】_百度贴吧...

这段代码展示了如何使用SQLite数据库在Android中进行操作,包括初始化数据库、插入用户信息、按隐私状态获取所有用户、更新用户信息以及删除用户。方法如insert()用于插入记录,getAllUser()根据隐私设置返回用户列表,modify()用于更新用户数据,而delete()则用于删除指定ID的用户。
摘要由CSDN通过智能技术生成

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

public DBHelper(Context context) {

this.context = context;

}

public void openDatabase() {

if(dbInstance == null) {

myDBHelper = new MyDBHelper(context,DB_DBNAME,VERSION);

//dbInstance = myDBHelper.getWritableDatabase();

dbInstance=SQLiteDatabase.openDatabase(databaseFilename, null, SQLiteDatabase.OPEN_READONLY);

}

}

public long insert(User user) {

ContentValues values = new ContentValues();

values.put("name", user.username);

System.out.println("user.name--"+user.username);

values.put("mobilephone", user.mobilePhone);

values.put("officephone", user.officePhone);

values.put("familyphone", user.familyPhone);

values.put("address", user.address);

values.put("othercontact", user.otherContact);

values.put("email",user.email);

values.put("position", user.position);

values.put("company", user.company);

values.put("zipcode", user.zipCode);

values.put("remark", user.remark);

values.put("imageid", user.imageId);

values.put("privacy", user.privacy);

System.out.println("user.mobilephone"+user.mobilePhone);

dbInstance=SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);

String dbpa= dbInstance.getPath();

System.out.println("dbpa="+dbpa);

return dbInstance.insert(DB_TABLENAME, null, values);

}

public ArrayList getAllUser(boolean privacy) {

ArrayList list = new ArrayList();

Cursor cursor=null;

System.out.println("come getAllUser");

File myDataPath = new File(DATABASE_PATH);

String databaseFilename = myDataPath+ "/" + DATABASE_FILENAME;

SQLiteDatabase dbInstance = SQLiteDatabase.openDatabase(databaseFilename, null, VERSION);

//dbInstance = myDBHelper.getWritableDatabase();

if(privacy) {

System.out.println("come getAllUser if");

cursor = dbInstance.query(DB_TABLENAME,

new String[]{"_id","name","mobilephone","officephone","familyphone","address","othercontact","email","position","company","zipcode","remark","imageid"},

"privacy=1",

null,

null,

null,

"name");

} else {

System.out.println("come getAllUser else");

cursor = dbInstance.query(

DB_TABLENAME,

new String[]{"_id","name","mobilephone","officephone","familyphone","address","othercontact","email","position","company","zipcode","remark","imageid"},

"privacy=0",

null,

null,

null,

"name");

}

System.out.println("finished getAllUser if else");

while(cursor.moveToNext()) {

HashMap item = new HashMap();

item.put("_id", cursor.getInt(cursor.getColumnIndex("_id")));

item.put("name", cursor.getString(cursor.getColumnIndex("name")));

item.put("mobilephone", cursor.getString(cursor.getColumnIndex("mobilephone")));

item.put("officephone", cursor.getString(cursor.getColumnIndex("officephone")));

item.put("familyphone", cursor.getString(cursor.getColumnIndex("familyphone")));

item.put("address", cursor.getString(cursor.getColumnIndex("address")));

item.put("othercontact", cursor.getString(cursor.getColumnIndex("othercontact")));

item.put("email", cursor.getString(cursor.getColumnIndex("email")));

item.put("position", cursor.getString(cursor.getColumnIndex("position")));

item.put("company", cursor.getString(cursor.getColumnIndex("company")));

item.put("zipcode", cursor.getString(cursor.getColumnIndex("zipcode")));

item.put("remark", cursor.getString(cursor.getColumnIndex("remark")));

item.put("imageid", cursor.getInt(cursor.getColumnIndex("imageid")));

list.add(item);

}

cursor.close();

return list;

}

public void modify(User user) {

ContentValues values = new ContentValues();

values.put("name", user.username);

values.put("mobilephone", user.mobilePhone);

values.put("officephone", user.officePhone);

values.put("familyphone", user.familyPhone);

values.put("address", user.address);

values.put("othercontact", user.otherContact);

values.put("email",user.email);

values.put("position", user.position);

values.put("company", user.company);

values.put("zipcode", user.zipCode);

values.put("remark", user.remark);

values.put("imageid", user.imageId);

System.out.println("user.username="+user.username);

System.out.println("user.mobilePhone="+user.mobilePhone);

dbInstance=SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);

System.out.println("dbInstance.toString()="+dbInstance.toString());

dbInstance.update(DB_TABLENAME, values, "_id=?", new String[]{String.valueOf(user._id)});

}

public void delete(int _id) {

dbInstance=SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);

dbInstance.delete(DB_TABLENAME, "_id=?", new String[]{String.valueOf(_id)});

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值