Skip to content

Commit 67e46ec

Browse files
committed
关闭数据库操作中的cursor
1 parent d133550 commit 67e46ec

File tree

8 files changed

+43
-21
lines changed

8 files changed

+43
-21
lines changed

res/layout/pull_to_refresh_header.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
android:layout_height="wrap_content"
5656
android:layout_gravity="center"
5757
android:gravity="center"
58+
android:textColor="@color/white"
5859
/>
5960
<TextView
6061
android:id="@+id/pull_to_refresh_updated_at"

src/com/dreamteam/app/commons/SectionHelper.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
import android.content.ContentValues;
66
import android.database.sqlite.SQLiteDatabase;
77

8-
import com.dreamteam.app.db.DbManager;
8+
import com.dreamteam.app.db.DbConstant;
99
import com.dreamteam.app.utils.FileUtils;
1010
import com.dreamteam.app.utils.MD5;
1111

1212
public class SectionHelper
1313
{
14+
1415
public static void removeRecord(SQLiteDatabase db, String url)
1516
{
16-
db.delete(DbManager.SECTION_TABLE_NAME, "url=?", new String[]{url});
17+
db.delete(DbConstant.SECTION_TABLE_NAME, "url=?", new String[]{url});
1718
db.close();
1819
}
1920

@@ -23,7 +24,7 @@ public static void insert(SQLiteDatabase db, String tableName, String title, Str
2324
values.put("table_name", tableName);
2425
values.put("title", title);
2526
values.put("url", url);
26-
db.insert(DbManager.SECTION_TABLE_NAME, null, values);
27+
db.insert(DbConstant.SECTION_TABLE_NAME, null, values);
2728
}
2829

2930
public static File newSdCache(String url)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.dreamteam.app.db;
2+
3+
public class DbConstant
4+
{
5+
public static final String FAVO_TABLE_NAME = "favorite_item";
6+
public static final String SECTION_TABLE_NAME = "section";
7+
8+
9+
}

src/com/dreamteam/app/db/DbManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
public class DbManager extends SQLiteOpenHelper
1414
{
1515
public static final String DB_NAME = "reader.db";
16-
public static final String SECTION_TABLE_NAME = "section";
1716
public static final String FAVORITE_ITEM_TABLE_NAME = "favorite_item";
1817

1918
private static final String CREATE_SECTION_TABLE =
20-
"create table" + " " + SECTION_TABLE_NAME
19+
"create table" + " " + DbConstant.SECTION_TABLE_NAME
2120
+ "(title text, url text, table_name text)";
2221

2322
private static final String CREATE_FAVORITE_TABLE = "create table"

src/com/dreamteam/app/db/FavoItemDbHelper.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,30 @@
55

66

77
public class FavoItemDbHelper
8-
{
8+
{
9+
public static final String TITLE = "title";
10+
public static final String PUBDATE = "pubdate";
11+
public static final String DETAIL = "item_detail";
12+
public static final String LINK = "link";
13+
public static final String FIRST_IMG_URL = "first_img_url";
14+
public static final String SECTION_TITLE = "table_name";
15+
public static final String SECTION_URL = "table_url";
16+
17+
918
public static void insert(SQLiteDatabase db, String title,
1019
String pubdate, String itemDetail, String link,
1120
String firstImgUrl, String sectionTitle, String sectionUrl)
1221
{
1322
ContentValues values = new ContentValues();
14-
values.put("title", title);
15-
values.put("pubdate", pubdate);
16-
values.put("item_detail", itemDetail);
17-
values.put("link", link);
18-
values.put("first_img_url", firstImgUrl);
19-
values.put("table_name", sectionTitle);
20-
values.put("table_url", sectionUrl);
23+
values.put(TITLE, title);
24+
values.put(PUBDATE, pubdate);
25+
values.put(DETAIL, itemDetail);
26+
values.put(LINK, link);
27+
values.put(FIRST_IMG_URL, firstImgUrl);
28+
values.put(SECTION_TITLE, sectionTitle);
29+
values.put(SECTION_URL, sectionUrl);
2130

22-
db.insert("favorite_item", null, values);
31+
db.insert(DbConstant.FAVO_TABLE_NAME, null, values);
2332
db.close();
2433
}
2534

src/com/dreamteam/app/ui/CategoryDetail.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ private void initData()
7171
cursor.moveToNext();
7272
}
7373
}
74+
cursor.close();
7475
db.close();
7576
//设置适配器
7677
mAdapter = new CategoryDetailAdapter(this, feeds, tableName);

src/com/dreamteam/app/ui/Main.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.dreamteam.app.commons.SectionHelper;
4040
import com.dreamteam.app.commons.SeriaHelper;
4141
import com.dreamteam.app.commons.UIHelper;
42+
import com.dreamteam.app.db.DbConstant;
4243
import com.dreamteam.app.db.DbManager;
4344
import com.dreamteam.app.entity.ItemListEntity;
4445
import com.dreamteam.app.entity.Section;
@@ -86,7 +87,7 @@ protected void onCreate(Bundle savedInstanceState)
8687
initBroadcast();
8788
checkDeprecated();
8889
}
89-
90+
9091
//检查是否来自shortcut的动作
9192
private void checkShortcutMsg()
9293
{
@@ -116,8 +117,8 @@ public void onReceive(Context context, Intent intent)
116117
if (action.equals(ACTION_ADD_SECTION))
117118
{
118119
// 最后一个adapter为空或已满,新生一个gridView
119-
Log.d(tag, gridAdapters.size() + "adapters");
120-
Log.d(tag, gridViews.size() + "views");
120+
// Log.d(tag, gridAdapters.size() + "adapters");
121+
// Log.d(tag, gridViews.size() + "views");
121122
GridAdapter lastGridAdapter = getLastGridAdapter();
122123
if (lastGridAdapter == null || lastGridAdapter.isFull())
123124
{
@@ -189,7 +190,7 @@ private Section getNewSection()
189190
Section section = new Section();
190191
DbManager mgr = new DbManager(Main.this, DbManager.DB_NAME, null, 1);
191192
SQLiteDatabase db = mgr.getWritableDatabase();
192-
Cursor cursor = db.query(DbManager.SECTION_TABLE_NAME, null, null, null,
193+
Cursor cursor = db.query(DbConstant.SECTION_TABLE_NAME, null, null, null,
193194
null, null, null);
194195
if (cursor.moveToLast())
195196
{
@@ -201,6 +202,7 @@ private Section getNewSection()
201202
section.setUrl(url);
202203
section.setTableName(tableName);
203204
}
205+
cursor.close();
204206
db.close();
205207
return section;
206208
}
@@ -497,7 +499,7 @@ private ArrayList<Section> readSections(int page) throws Exception
497499
// 从数据库读数据
498500
DbManager mgr = new DbManager(Main.this, DbManager.DB_NAME, null, 1);
499501
SQLiteDatabase db = mgr.getWritableDatabase();
500-
Cursor cursor = db.query(DbManager.SECTION_TABLE_NAME,
502+
Cursor cursor = db.query(DbConstant.SECTION_TABLE_NAME,
501503
null, null, null, null, null, null);
502504
len = cursor.getCount();
503505
db.close();
@@ -569,11 +571,12 @@ private int getPageSize()
569571
// 从数据库读数据
570572
DbManager mgr = new DbManager(Main.this, DbManager.DB_NAME, null, 1);
571573
SQLiteDatabase db = mgr.getWritableDatabase();
572-
Cursor cursor = db.query(DbManager.SECTION_TABLE_NAME,
574+
Cursor cursor = db.query(DbConstant.SECTION_TABLE_NAME,
573575
null, null, null, null, null, null);
574576
// pager分页
575577
int pageSize = 0;
576578
int sectionCount = cursor.getCount();
579+
cursor.close();
577580
db.close();
578581

579582
if (sectionCount % PAGE_SECTION_SIZE == 0)

src/com/dreamteam/app/utils/DateUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.dreamteam.app.utils;
22

3-
import java.util.Date;
43

54
/**
65
* @description

0 commit comments

Comments
 (0)