Skip to content

Commit

Permalink
更新数据库
Browse files Browse the repository at this point in the history
  • Loading branch information
iMeiji committed Dec 7, 2016
1 parent 529907b commit 345f9e0
Show file tree
Hide file tree
Showing 16 changed files with 328 additions and 275 deletions.
4 changes: 1 addition & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName '1.3.5'
versionName '1.4'
}
buildTypes {
release {
Expand All @@ -30,8 +30,6 @@ android {
debuggable true
}
}
productFlavors {
}
}

dependencies {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".InitApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/meiji/daily/AddActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.afollestad.materialdialogs.MaterialDialog;
import com.google.gson.Gson;
import com.meiji.daily.bean.ZhuanlanBean;
import com.meiji.daily.dao.ZhuanlanDao;
import com.meiji.daily.database.dao.ZhuanlanDao;
import com.meiji.daily.utils.Api;

import java.io.IOException;
Expand All @@ -32,7 +32,7 @@
public class AddActivity extends BaseActivity {

private Gson gson = new Gson();
private ZhuanlanDao dao = new ZhuanlanDao(this);
private ZhuanlanDao zhuanlanDao = new ZhuanlanDao();
private boolean result = false;
private MaterialDialog materialDialog;
private Handler handler = new Handler(new Handler.Callback() {
Expand Down Expand Up @@ -79,7 +79,7 @@ private void handleSendText(String shareText) {
final Matcher matcher = Pattern.compile(regex).matcher(shareText);
if (matcher.find()) {
String slug = matcher.group(1);
List<ZhuanlanBean> query = dao.query(TYPE_USERADD);
List<ZhuanlanBean> query = zhuanlanDao.query(TYPE_USERADD);
for (ZhuanlanBean bean : query) {
if (bean.getSlug().equals(slug)) {
onFinish(getString(R.string.has_been_added));
Expand All @@ -103,7 +103,7 @@ public void run() {
String postsCount = String.valueOf(bean.getPostsCount());
String intro = bean.getIntro();
String slug = bean.getSlug();
result = dao.add(type, avatarUrl, avatarId, name, followersCount, postsCount, intro, slug);
result = zhuanlanDao.add(type, avatarUrl, avatarId, name, followersCount, postsCount, intro, slug);
if (result) {
Message message = handler.obtainMessage(1);
message.sendToTarget();
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/meiji/daily/InitApp.java
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();
}
}
92 changes: 0 additions & 92 deletions app/src/main/java/com/meiji/daily/dao/ZhuanlanDao.java

This file was deleted.

59 changes: 59 additions & 0 deletions app/src/main/java/com/meiji/daily/database/DatabaseHelper.java
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 app/src/main/java/com/meiji/daily/database/dao/ZhuanlanDao.java
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;
}
}
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) ";
}
Loading

0 comments on commit 345f9e0

Please sign in to comment.