-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
iMeiji
committed
Dec 7, 2016
1 parent
529907b
commit 345f9e0
Showing
16 changed files
with
328 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.meiji.daily; | ||
|
||
import android.app.Application; | ||
import android.content.Context; | ||
|
||
/** | ||
* Created by Meiji on 2016/12/7. | ||
*/ | ||
|
||
public class InitApp extends Application { | ||
|
||
public static Context AppContext; | ||
|
||
@Override | ||
public void onCreate() { | ||
super.onCreate(); | ||
AppContext = getApplicationContext(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
app/src/main/java/com/meiji/daily/database/DatabaseHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.meiji.daily.database; | ||
|
||
import android.content.Context; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.database.sqlite.SQLiteOpenHelper; | ||
|
||
import com.meiji.daily.InitApp; | ||
import com.meiji.daily.database.table.ZhuanlanTable; | ||
|
||
/** | ||
* Created by Meiji on 2016/12/7. | ||
*/ | ||
|
||
public class DatabaseHelper extends SQLiteOpenHelper { | ||
|
||
private static final String DB_NAME = "Daily"; | ||
private static final int DB_VERSION = 2; | ||
private static final String CLEAR_TABLE_DATA = "delete from "; | ||
private static final String DROP_TABLE = "drop table if exists "; | ||
private static DatabaseHelper instance = null; | ||
private static SQLiteDatabase db = null; | ||
|
||
private DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { | ||
super(context, name, factory, version); | ||
} | ||
|
||
private static synchronized DatabaseHelper getInstance() { | ||
if (instance == null) { | ||
instance = new DatabaseHelper(InitApp.AppContext, DB_NAME, null, DB_VERSION); | ||
} | ||
return instance; | ||
} | ||
|
||
public static synchronized SQLiteDatabase getDatabase() { | ||
if (db == null) { | ||
db = getInstance().getWritableDatabase(); | ||
} | ||
return db; | ||
} | ||
|
||
public static synchronized void closeDatabase() { | ||
if (db != null) { | ||
db.close(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onCreate(SQLiteDatabase db) { | ||
db.execSQL(ZhuanlanTable.CREATE_TABLE); | ||
} | ||
|
||
@Override | ||
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { | ||
if (oldVersion == 1 && newVersion == 2) { | ||
db.execSQL(DROP_TABLE + ZhuanlanTable.TABLENAME); | ||
db.execSQL(ZhuanlanTable.CREATE_TABLE); | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
app/src/main/java/com/meiji/daily/database/dao/ZhuanlanDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.meiji.daily.database.dao; | ||
|
||
import android.content.ContentValues; | ||
import android.database.Cursor; | ||
import android.database.sqlite.SQLiteDatabase; | ||
|
||
import com.meiji.daily.bean.ZhuanlanBean; | ||
import com.meiji.daily.database.DatabaseHelper; | ||
import com.meiji.daily.database.table.ZhuanlanTable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static android.R.attr.id; | ||
|
||
/** | ||
* Created by Meiji on 2016/11/25. | ||
*/ | ||
|
||
public class ZhuanlanDao { | ||
|
||
private SQLiteDatabase db; | ||
|
||
public ZhuanlanDao() { | ||
this.db = DatabaseHelper.getDatabase(); | ||
} | ||
|
||
public boolean add( | ||
String type, | ||
String avatarUrl, | ||
String avatarId, | ||
String name, | ||
String followersCount, | ||
String postsCount, | ||
String intro, | ||
String slug) { | ||
ContentValues values = new ContentValues(); | ||
values.put(ZhuanlanTable.TYPE, type); | ||
values.put(ZhuanlanTable.AVATARURL, avatarUrl); | ||
values.put(ZhuanlanTable.AVATARId, avatarId); | ||
values.put(ZhuanlanTable.NAME, name); | ||
values.put(ZhuanlanTable.FOLLOWERSCOUNT, followersCount); | ||
values.put(ZhuanlanTable.POSTSCOUNT, postsCount); | ||
values.put(ZhuanlanTable.INTRO, intro); | ||
values.put(ZhuanlanTable.SLUG, slug); | ||
long result = db.insert(ZhuanlanTable.TABLENAME, null, values); | ||
return result != -1; | ||
} | ||
|
||
public List<ZhuanlanBean> query(int type) { | ||
SQLiteDatabase db = DatabaseHelper.getDatabase(); | ||
Cursor cursor = db.query(ZhuanlanTable.TABLENAME, null, "type=?", new String[]{type + ""}, null, null, null); | ||
List<ZhuanlanBean> list = new ArrayList<>(); | ||
while (cursor.moveToNext()) { | ||
ZhuanlanBean bean = new ZhuanlanBean(); | ||
ZhuanlanBean.AvatarBeanX avatarBeanX = new ZhuanlanBean.AvatarBeanX(); | ||
avatarBeanX.setTemplate(cursor.getString(ZhuanlanTable.ID_AVATARURL)); | ||
avatarBeanX.setId(cursor.getString(ZhuanlanTable.ID_AVATARId)); | ||
bean.setAvatar(avatarBeanX); | ||
bean.setName(cursor.getString(ZhuanlanTable.ID_NAME)); | ||
bean.setFollowersCount(Integer.parseInt(cursor.getString(ZhuanlanTable.ID_FOLLOWERSCOUNT))); | ||
bean.setPostsCount(Integer.parseInt(cursor.getString(ZhuanlanTable.ID_POSTSCOUNT))); | ||
bean.setIntro(cursor.getString(ZhuanlanTable.ID_INTRO)); | ||
bean.setSlug(cursor.getString(ZhuanlanTable.ID_SLUG)); | ||
list.add(bean); | ||
} | ||
cursor.close(); | ||
return list; | ||
} | ||
|
||
public boolean removeSlug(String slug) { | ||
SQLiteDatabase db = DatabaseHelper.getDatabase(); | ||
db.delete(ZhuanlanTable.TABLENAME, "slug=?", new String[]{slug}); | ||
return id != -1; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
app/src/main/java/com/meiji/daily/database/table/ZhuanlanTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.meiji.daily.database.table; | ||
|
||
/** | ||
* Created by Meiji on 2016/12/7. | ||
*/ | ||
|
||
public class ZhuanlanTable { | ||
|
||
/** | ||
* 专栏信息表 | ||
*/ | ||
public static final String TABLENAME = "ZhuanlanTable"; | ||
|
||
/** | ||
* 字段部分 | ||
*/ | ||
public static final String TYPE = "type"; | ||
public static final String AVATARURL = "avatarUrl"; | ||
public static final String AVATARId = "avatarId"; | ||
public static final String NAME = "name"; | ||
public static final String FOLLOWERSCOUNT = "followersCount"; | ||
public static final String POSTSCOUNT = "postsCount"; | ||
public static final String INTRO = "intro"; | ||
public static final String SLUG = "slug"; | ||
|
||
/** | ||
* 字段ID 数据库操作建立字段对应关系 从0开始 | ||
*/ | ||
public static final int ID_TYPE = 0; | ||
public static final int ID_AVATARURL = 1; | ||
public static final int ID_AVATARId = 2; | ||
public static final int ID_NAME = 3; | ||
public static final int ID_FOLLOWERSCOUNT = 4; | ||
public static final int ID_POSTSCOUNT = 5; | ||
public static final int ID_INTRO = 6; | ||
public static final int ID_SLUG = 7; | ||
|
||
public static final String CREATE_TABLE = "create table " + TABLENAME + "(" + | ||
TYPE + " text," + | ||
AVATARURL + " text," + | ||
AVATARId + " text," + | ||
NAME + " text," + | ||
FOLLOWERSCOUNT + " text," + | ||
POSTSCOUNT + " text," + | ||
INTRO + " text," + | ||
SLUG + " text primary key) "; | ||
} |
Oops, something went wrong.