02数据库的设计

新建domain包和db包,domain包下存放User实体类。代码如下:

package com.dy.ustc.im.domain;

import com.easemob.chat.EMContact;

/**
 * 
 * 用户信息封装
 * 
 * @author
 * 
 */

public class User extends EMContact {

	private int unreadMsgCount;

	private String header;

	public int getUnreadMsgCount() {
		return unreadMsgCount;
	}

	public void setUnreadMsgCount(int unreadMsgCount) {
		this.unreadMsgCount = unreadMsgCount;
	}

	public String getHeader() {
		return header;
	}

	public void setHeader(String header) {
		this.header = header;
	}

	@Override
	public int hashCode() {
		return 17 * getUsername().hashCode();
	}

	@Override
	public boolean equals(Object o) {
		if (o == null || !(o instanceof User)) {
			return false;
		}
		return getUsername().equals(((User) o).getUsername());
	}

	@Override
	public String toString() {
		return nick == null ? username : nick;
	}

}

包含两部分内容:

一是用户的未读的消息的数目,而是用户的用户名。

特别留意下,本实体类重写了hashCode和equal方法,代码如下:

	@Override
	public int hashCode() {
		return 17 * getUsername().hashCode();
	}

	@Override
	public boolean equals(Object o) {
		if (o == null || !(o instanceof User)) {
			return false;
		}
		return getUsername().equals(((User) o).getUsername());
	}

DbOpenHelper继承自:SQLiteOpenHelper,默认的有创建数据库和升级数据库的代码,在构造器中初始化,代码如下:

package com.dy.ustc.im.db;


import com.dy.ustc.im.application.IMApplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbOpenHelper extends SQLiteOpenHelper {
	
	private static final int DATABASE_VERSION = 1;
	
	private static DbOpenHelper instance; 
	
	
	private static final String USERNAME_TABLE_CREATE = "CREATE TABLE "
			+ UserDao.TABLE_NAME + " ("
			+ UserDao.COLUMN_NAME_NICK +" TEXT, "
			+ UserDao.COLUMN_NAME_ID + " TEXT PRIMARY KEY);";
	
	private static final String INIVTE_MESSAGE_TABLE_CREATE = "CREATE TABLE "
			+ InviteMessgeDao.TABLE_NAME + " ("
			+ InviteMessgeDao.COLUMN_NAME_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ InviteMessgeDao.COLUMN_NAME_FROM + " TEXT, "
			+ InviteMessgeDao.COLUMN_NAME_GROUP_ID + " TEXT, "
			+ InviteMessgeDao.COLUMN_NAME_GROUP_Name + " TEXT, "
			+ InviteMessgeDao.COLUMN_NAME_REASON + " TEXT, "
			+ InviteMessgeDao.COLUMN_NAME_STATUS + " INTEGER, "
			+ InviteMessgeDao.COLUMN_NAME_ISINVITEFROMME + " INTEGER, "
			+ InviteMessgeDao.COLUMN_NAME_TIME + " TEXT); ";
			
	

	public DbOpenHelper(Context context) {
		super(context, getUserDatabaseName(), null, DATABASE_VERSION);
	}

	
	private static String getUserDatabaseName(){
		
		return IMApplication.getInstance().getUserName() + ".db";
		
	}
	
	
	
	public static DbOpenHelper getInstance(Context context) {
		if (instance == null) {
			instance = new DbOpenHelper(context.getApplicationContext());
		}
		return instance;
	}


	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(USERNAME_TABLE_CREATE);
		db.execSQL(INIVTE_MESSAGE_TABLE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
	}

	public void closeDB(){
		if (instance != null) {
			try {
				SQLiteDatabase db = instance.getWritableDatabase();
				db.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			instance = null;
		}
	}
	
}

UserDao类封装增删改查的方法,代码如下:

package com.dy.ustc.im.db;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dy.ustc.im.Constant;
import com.dy.ustc.im.domain.User;
import com.easemob.util.HanziToPinyin;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

public class UserDao {

	public static final String TABLE_NAME = "uers";
	public static final String COLUMN_NAME_ID = "username";
	public static final String COLUMN_NAME_NICK = "nick";
	public static final String COLUMN_NAME_IS_STRANGER = "is_stranger";

	private DbOpenHelper dbHelper;

	public UserDao(Context context) {
		dbHelper = DbOpenHelper.getInstance(context);
	}

	/**
	 * 保存好友List
	 * 
	 * @param contactList
	 */
	public void saveContactList(List contactList) {

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		if (db.isOpen()) {
			db.delete(TABLE_NAME, null, null);
			for (User user : contactList) {
				ContentValues values = new ContentValues();
				values.put(COLUMN_NAME_ID, user.getUsername());
				if (user.getNick() != null) {
					values.put(COLUMN_NAME_NICK, user.getNick());
				}
				db.replace(TABLE_NAME, null, values);
			}
		}

	}

	/**
	 * 
	 * 获取好友list
	 * 
	 * @return
	 */
	public Map getContactList() {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Map users = new HashMap();
		if (db.isOpen()) {
			Cursor cursor = db.rawQuery(
					"select * from " + TABLE_NAME /* + " desc" */, null);
			while (cursor.moveToNext()) {
				String username = cursor.getString(cursor
						.getColumnIndex(COLUMN_NAME_ID));
				String nick = cursor.getString(cursor
						.getColumnIndex(COLUMN_NAME_NICK));

				User user = new User();
				user.setUsername(username);
				String headerName = null;
				if (!TextUtils.isEmpty(user.getNick())) {
					headerName = user.getNick();
				} else {
					headerName = user.getUsername();
				}
				if (username.equals(Constant.NEW_FRIENDS_USERNAME)
						|| username.equals(Constant.GROUP_USERNAME)) {

					user.setHeader("");

				} else if (Character.isDigit(headerName.charAt(0))) {
					user.setHeader("#");
				} else {
					user.setHeader(HanziToPinyin.getInstance()
							.get(headerName.substring(0, 1)).get(0).target
							.substring(0, 1).toUpperCase());
					char header = user.getHeader().toLowerCase().charAt(0);
					if (header < 'a' || header > 'z') {
						user.setHeader("#");
					}
				}
				users.put(username, user);
			}
			cursor.close();

		}
		return users;
	}

	/**
	 * 删除一个联系人
	 * 
	 * @param username
	 */
	public void deleteContact(String username) {

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		if (db.isOpen()) {
			db.delete(TABLE_NAME, COLUMN_NAME_ID + " = ?",
					new String[] { username });
		}

	}

	/**
	 * 
	 * 保存一个联系人
	 * 
	 * @param user
	 */
	public void saveContact(User user) {

		SQLiteDatabase db = dbHelper.getWritableDatabase();

		ContentValues values = new ContentValues();

		values.put(COLUMN_NAME_ID, user.getUsername());
		if (user.getNick() != null) {
			values.put(COLUMN_NAME_NICK, user.getNick());
		}
		if (db.isOpen()) {
			db.replace(TABLE_NAME, null, values);
		}

	}

}


本案例的代码不熟悉没关系,后面会用到,到时候自然明白。



你可能感兴趣的:(IM即时通讯项目)