个人记录
  1 public class DataBaseContentProvider extends ContentProvider {
  2 
  3     private SQLiteOpenHelper mSQLiteOpenHelper;
  4 
  5     private SQLiteDatabase db;
  6     /**
  7      * URI
  8      */
  9     private static final UriMatcher URIMATCHER = new UriMatcher(
 10             UriMatcher.NO_MATCH);
 11 
 12     private static final int URI_MATCH_USER = 0;
 13 
 14     private static final int URI_MATCH_FRIEND = 1;
 15 
 16     private static final int URI_MATCH_TALK = 2;
 17 
 18     private static final int URI_MATCH_COMMENT = 3;
 19     
 20     private static final int URI_MATCH_ALARM = 4;
 21 
 22     static {
 23         URIMATCHER.addURI(Constants.AUTHORITY, FriendTable.TABLE_NAME,
 24                 URI_MATCH_FRIEND);
 25 
 26         URIMATCHER.addURI(Constants.AUTHORITY, UserTable.TABLE_NAME,
 27                 URI_MATCH_USER);
 28 
 29         URIMATCHER.addURI(Constants.AUTHORITY, TalkTable.TABLE_NAME,
 30                 URI_MATCH_TALK);
 31 
 32         URIMATCHER.addURI(Constants.AUTHORITY, CommentTable.TABLE_NAME,
 33                 URI_MATCH_COMMENT);
 34         
 35         URIMATCHER.addURI(Constants.AUTHORITY,AlarmTable.TABLE_NAME,
 36                 URI_MATCH_ALARM);
 37 
 38     }
 39 
 40     @Override
 41     public boolean onCreate() {
 42         // TODO Auto-generated method stub
 43         mSQLiteOpenHelper = DataBaseHelper.getInstance(getContext());
 44         db = mSQLiteOpenHelper.getWritableDatabase();
 45         return true;
 46     }
 47 
 48     /**
 49      * 返回要查询的表名
 50      */
 51     private String getTableNameByUri(Uri uri) {
 52         if (uri == null) {
 53             return null;
 54         }
 55         int match = URIMATCHER.match(uri);
 56         switch (match) {
 57         case URI_MATCH_USER:
 58             return UserTable.TABLE_NAME;
 59 
 60         case URI_MATCH_FRIEND:
 61             return FriendTable.TABLE_NAME;
 62 
 63         case URI_MATCH_TALK:
 64             return TalkTable.TABLE_NAME;
 65 
 66         case URI_MATCH_COMMENT:
 67             return CommentTable.TABLE_NAME;
 68             
 69         case URI_MATCH_ALARM:
 70             return AlarmTable.TABLE_NAME;
 71 
 72         default:
 73             throw new IllegalArgumentException(
 74                     "DataBaseContentProvider getTableNameByUri");
 75         }
 76     }
 77 
 78     @Override
 79     public int delete(Uri url, String where, String[] whereArgs) {
 80         // TODO Auto-generated method stub
 81         int match = URIMATCHER.match(url);
 82         if (match == UriMatcher.NO_MATCH) {
 83             throw new IllegalArgumentException("Unknown URL");
 84         }
 85 
 86         int count = db.delete(getTableNameByUri(url), where, whereArgs);
 87         ContentResolver cr = getContext().getContentResolver();
 88         cr.notifyChange(url, null);
 89         return count;
 90     }
 91 
 92     @Override
 93     public String getType(Uri arg0) {
 94         // TODO Auto-generated method stub
 95 
 96         switch (URIMATCHER.match(arg0)) {
 97         case URI_MATCH_USER:
 98             return UserTable.CONTENT_TYPE;
 99         case URI_MATCH_FRIEND:
100             return FriendTable.CONTENT_TYPE;
101         case URI_MATCH_TALK:
102             return TalkTable.CONTENT_TYPE;
103         case URI_MATCH_COMMENT:
104             return CommentTable.CONTENT_TYPE;
105         case URI_MATCH_ALARM:
106             return AlarmTable.CONTENT_TYPE;
107         default:
108             throw new IllegalArgumentException(
109                     "DataBaseContentProvider getType");
110         }
111     }
112 
113     @Override
114     public Uri insert(Uri uri, ContentValues values) {
115         // TODO Auto-generated method stub
116 
117         int match = URIMATCHER.match(uri);
118         Uri rtUri = null;
119         long rowId = db.replace(getTableNameByUri(uri), null, values);
120         if (rowId > 0) {
121             rtUri = ContentUris.withAppendedId(uri, rowId);
122         }
123         ContentResolver cr = getContext().getContentResolver();
124         cr.notifyChange(uri, null);
125         return rtUri;
126     }
127 
128     @Override
129     public Cursor query(Uri uri, String[] projection, String selection,
130             String[] selectionArgs, String sortOrder) {
131         // TODO Auto-generated method stub
132         String tableName = this.getTableNameByUri(uri);
133         Cursor c = db.query(tableName, projection, selection, selectionArgs,
134                 null, null, sortOrder, null);
135         c.setNotificationUri(getContext().getContentResolver(), uri);
136         return c;
137     }
138 
139     @Override
140     public int update(Uri uri, ContentValues values, String selection,
141             String[] selectionArgs) {
142         // TODO Auto-generated method stub
143         String tableName = this.getTableNameByUri(uri);
144         int rt = db.update(tableName, values, selection, selectionArgs);
145         ContentResolver cr = getContext().getContentResolver();
146         cr.notifyChange(uri, null);
147         return rt;
148     }
149 
150 }
View Code
<provider    android:name="com.winning.cqs.pregnantbabycare_cqs_v1.db.DataBaseContentProvider"
            android:authorities="com.winning.cqs.pregnantbabycare_cqs_v1"
            android:multiprocess="true" >
</provider>
View Code
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    public int _id;
    public String hospitalHost;
    public String hospitalName;
    public String lastModify;
    public String idNo;
    public String mobile;
    public String name;
    public String hospitalCode;
    public String dueDate;
    public Integer activity;
    public Integer yxtUserID;
    public String nick;
    public String cardTypeDis;
    public Integer hospitalID;
    public int id;
    public Integer yxtMemberID;
    public Integer defaultPatientID;
    public String address;
    public Integer lastPregnancyID;
    public String createDate;
    public String cardNo;
    public String medicalNo;
    public Integer patientID;

    public static String saveUserToDB(ContentResolver cr, User user) {
        if (null == cr || null == user) {
            return null;
        }
        String result = null;
        //获取到还没有被修改过的用户信息
        User user2 = getUsers(cr, String.valueOf(user.id));
        ContentValues values = new ContentValues();

        values.put(UserTable.Activity, user.activity);
        values.put(UserTable.Address, user.address);
        values.put(UserTable.CardNo, user.cardNo);
        values.put(UserTable.CardTypeDis, user.cardTypeDis);
        values.put(UserTable.CreateDate, user.createDate);
        values.put(UserTable.DefaultPatientID, user.defaultPatientID);
        values.put(UserTable.DueDate, user.dueDate);
        values.put(UserTable.HospitalCode, user.hospitalCode);
        values.put(UserTable.HospitalHost, user.hospitalHost);
        values.put(UserTable.HospitalID, user.hospitalID);
        values.put(UserTable.HospitalName, user.hospitalName);
        values.put(UserTable.Id, user.id);
        values.put(UserTable.IdNo, user.idNo);
        values.put(UserTable.LastModify, user.lastModify);
        values.put(UserTable.LastPregnancyID, user.lastPregnancyID);
        values.put(UserTable.MedicalNo, user.medicalNo);
        values.put(UserTable.Mobile, user.mobile);
        values.put(UserTable.Name, user.name);
        values.put(UserTable.Nick, user.nick);
        values.put(UserTable.PatientID, user.patientID);
        values.put(UserTable.YxtMemberID, user.yxtMemberID);
        values.put(UserTable.YxtUserID, user.yxtUserID);

        if (values.size() > 0) {
            String whereStr = UserTable.Id + " = " + user.id;
            // 查询是否有相同数据
            Cursor cursor = cr.query(UserTable.CONTENT_URI, null, whereStr,
                    null, null);
            if ((null != cursor) && (0 < cursor.getCount())) {
                cr.update(UserTable.CONTENT_URI, values, whereStr, null);
            } else {
                cr.insert(UserTable.CONTENT_URI, values);
            }
            if (cursor != null) {
                cursor.close();
            }
        }

        if (isUpdate(user, user2))
            result = "信息保存成功!";

        return result;
    }
    /**
     * 判断用户信息是否被修改
     * @param user1
     * @param user2
     * @return
     */
    public static boolean isUpdate(User user1, User user2) {
        boolean b = true;
        if (user1 == null || user2 == null) {
            return b;
        }
        if (user1.id == user2.id && user1.nick.equals(user2.nick)
                && user1.dueDate.equals(user2.dueDate)) {
            b = false;
        }
        return b;
    }

    public static User getUsers(ContentResolver cr, String id) {
        User friend = null;
        if (null != cr) {
            Cursor cursor = cr.query(UserTable.CONTENT_URI, null, UserTable.Id
                    + "=?", new String[] { id }, null);
            if (cursor.moveToFirst()) {
                friend = new User();

                friend._id = cursor
                        .getInt(cursor.getColumnIndex(UserTable._ID));
                friend.activity = cursor.getInt(cursor
                        .getColumnIndex(UserTable.Activity));
                friend.address = cursor.getString(cursor
                        .getColumnIndex(UserTable.Address));
                friend.cardNo = cursor.getString(cursor
                        .getColumnIndex(UserTable.CardNo));
                friend.cardTypeDis = cursor.getString(cursor
                        .getColumnIndex(UserTable.CardTypeDis));
                friend.createDate = cursor.getString(cursor
                        .getColumnIndex(UserTable.CreateDate));
                friend.defaultPatientID = cursor.getInt(cursor
                        .getColumnIndex(UserTable.DefaultPatientID));
                friend.dueDate = cursor.getString(cursor
                        .getColumnIndex(UserTable.DueDate));
                friend.hospitalCode = cursor.getString(cursor
                        .getColumnIndex(UserTable.HospitalCode));
                friend.hospitalHost = cursor.getString(cursor
                        .getColumnIndex(UserTable.HospitalHost));
                friend.hospitalID = cursor.getInt(cursor
                        .getColumnIndex(UserTable.HospitalID));
                friend.hospitalName = cursor.getString(cursor
                        .getColumnIndex(UserTable.HospitalName));
                friend.id = cursor.getInt(cursor.getColumnIndex(UserTable.Id));
                friend.idNo = cursor.getString(cursor
                        .getColumnIndex(UserTable.IdNo));
                friend.lastModify = cursor.getString(cursor
                        .getColumnIndex(UserTable.LastModify));
                friend.lastPregnancyID = cursor.getInt(cursor
                        .getColumnIndex(UserTable.LastPregnancyID));
                friend.medicalNo = cursor.getString(cursor
                        .getColumnIndex(UserTable.MedicalNo));
                friend.mobile = cursor.getString(cursor
                        .getColumnIndex(UserTable.Mobile));
                friend.name = cursor.getString(cursor
                        .getColumnIndex(UserTable.Name));
                friend.nick = cursor.getString(cursor
                        .getColumnIndex(UserTable.Nick));
                friend.patientID = cursor.getInt(cursor
                        .getColumnIndex(UserTable.PatientID));
                friend.yxtMemberID = cursor.getInt(cursor
                        .getColumnIndex(UserTable.YxtMemberID));
                friend.yxtUserID = cursor.getInt(cursor
                        .getColumnIndex(UserTable.YxtUserID));

                cursor.moveToNext();
                cursor.close();
            }

        }
        return friend;
    }

    public static ArrayList<User> getAllUsers(ContentResolver cr) {
        ArrayList<User> users = new ArrayList<User>();
        if (null != cr) {
            Cursor cursor = cr.query(UserTable.CONTENT_URI, null, null, null,
                    null);
            if (null != cursor) {
                int friendsCount = cursor.getCount();

                cursor.moveToFirst();
                for (int k = 0; k < friendsCount; k++) {
                    User friend = new User();

                    friend._id = cursor.getInt(cursor
                            .getColumnIndex(UserTable._ID));
                    friend.activity = cursor.getInt(cursor
                            .getColumnIndex(UserTable.Activity));
                    friend.address = cursor.getString(cursor
                            .getColumnIndex(UserTable.Address));
                    friend.cardNo = cursor.getString(cursor
                            .getColumnIndex(UserTable.CardNo));
                    friend.cardTypeDis = cursor.getString(cursor
                            .getColumnIndex(UserTable.CardTypeDis));
                    friend.createDate = cursor.getString(cursor
                            .getColumnIndex(UserTable.CreateDate));
                    friend.defaultPatientID = cursor.getInt(cursor
                            .getColumnIndex(UserTable.DefaultPatientID));
                    friend.dueDate = cursor.getString(cursor
                            .getColumnIndex(UserTable.DueDate));
                    friend.hospitalCode = cursor.getString(cursor
                            .getColumnIndex(UserTable.HospitalCode));
                    friend.hospitalHost = cursor.getString(cursor
                            .getColumnIndex(UserTable.HospitalHost));
                    friend.hospitalID = cursor.getInt(cursor
                            .getColumnIndex(UserTable.HospitalID));
                    friend.hospitalName = cursor.getString(cursor
                            .getColumnIndex(UserTable.HospitalName));
                    friend.id = cursor.getInt(cursor
                            .getColumnIndex(UserTable.Id));
                    friend.idNo = cursor.getString(cursor
                            .getColumnIndex(UserTable.IdNo));
                    friend.lastModify = cursor.getString(cursor
                            .getColumnIndex(UserTable.LastModify));
                    friend.lastPregnancyID = cursor.getInt(cursor
                            .getColumnIndex(UserTable.LastPregnancyID));
                    friend.medicalNo = cursor.getString(cursor
                            .getColumnIndex(UserTable.MedicalNo));
                    friend.mobile = cursor.getString(cursor
                            .getColumnIndex(UserTable.Mobile));
                    friend.name = cursor.getString(cursor
                            .getColumnIndex(UserTable.Name));
                    friend.nick = cursor.getString(cursor
                            .getColumnIndex(UserTable.Nick));
                    friend.patientID = cursor.getInt(cursor
                            .getColumnIndex(UserTable.PatientID));
                    friend.yxtMemberID = cursor.getInt(cursor
                            .getColumnIndex(UserTable.YxtMemberID));
                    friend.yxtUserID = cursor.getInt(cursor
                            .getColumnIndex(UserTable.YxtUserID));

                    users.add(friend);

                    cursor.moveToNext();
                }
                cursor.close();
            }

        }
        return users;
    }

    public static class UserTable implements BaseColumns {
        public static final String TABLE_NAME = "user";

        public static final Uri CONTENT_URI = Uri.parse("content://"
                + Constants.AUTHORITY + "/" + TABLE_NAME);

        public static final String CONTENT_TYPE = "vnd.android.cursor.dir/"
                + Constants.AUTHORITY + "/" + TABLE_NAME;

        public static final String _ID = "_id";
        public static final String HospitalHost = "hospitalHost";
        public static final String HospitalName = "hospitalName";
        public static final String LastModify = "lastModify";
        public static final String IdNo = "idNo";
        public static final String Mobile = "mobile";
        public static final String Name = "name";
        public static final String HospitalCode = "hospitalCode";
        public static final String DueDate = "dueDate";
        public static final String Activity = "activity";
        public static final String YxtUserID = "yxtUserID";
        public static final String Nick = "nick";
        public static final String CardTypeDis = "cardTypeDis";
        public static final String HospitalID = "hospitalID";
        public static final String Id = "id";
        public static final String YxtMemberID = "yxtMemberID";
        public static final String DefaultPatientID = "defaultPatientID";
        public static final String Address = "address";
        public static final String LastPregnancyID = "lastPregnancyID";
        public static final String CreateDate = "createDate";
        public static final String CardNo = "cardNo";
        public static final String MedicalNo = "medicalNo";
        public static final String PatientID = "patientID";

        public static final String TABLE_CREATESQL = "CREATE TABLE "
                + TABLE_NAME + " (" + "_id"
                + " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," + HospitalHost
                + " TEXT," + HospitalName + " TEXT," + LastModify + " TEXT,"
                + IdNo + " TEXT," + Mobile + " TEXT," + Name + " TEXT,"
                + HospitalCode + " TEXT," + DueDate + " TEXT," + Activity
                + " TEXT," + YxtUserID + " TEXT," + Nick + " TEXT,"
                + CardTypeDis + " TEXT," + HospitalID + " TEXT," + Id
                + " TEXT," + YxtMemberID + " TEXT," + DefaultPatientID
                + " TEXT," + Address + " TEXT," + LastPregnancyID + " TEXT,"
                + CreateDate + " TEXT," + CardNo + " TEXT," + MedicalNo
                + " TEXT," + PatientID + " INTEGER" + ");";

    }

}
View Code

 

posted on 2014-03-05 09:34  翻滚的咸鱼  阅读(418)  评论(0编辑  收藏  举报