Skip to content

Commit 971ffd3

Browse files
committed
add DAO layer
1 parent 5fe44ac commit 971ffd3

File tree

2 files changed

+93
-34
lines changed

2 files changed

+93
-34
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.dreamteam.app.dao;
2+
3+
import java.util.ArrayList;
4+
5+
import android.content.Context;
6+
import android.database.Cursor;
7+
import android.database.sqlite.SQLiteDatabase;
8+
import android.util.Log;
9+
10+
import com.dreamteam.app.db.DbConstant;
11+
import com.dreamteam.app.db.DbManager;
12+
import com.dreamteam.app.entity.Section;
13+
import com.dreamteam.app.ui.Main;
14+
15+
public class SectionDAO
16+
{
17+
public static final String tag = "SectionDAO";
18+
19+
public ArrayList<Section> getList(Context context, int page)
20+
{
21+
ArrayList<Section> list = null;
22+
int len = 0;// 表长
23+
int start = 0;// 其实读
24+
int end = 0;// 结尾
25+
Log.i(tag, "page = " + page);
26+
// 从数据库读数据
27+
DbManager mgr = new DbManager(context, DbManager.DB_NAME, null, 1);
28+
SQLiteDatabase db = mgr.getWritableDatabase();
29+
Cursor cursor = db.query(DbConstant.SECTION_TABLE_NAME,
30+
null, null, null, null, null, null);
31+
len = cursor.getCount();
32+
db.close();
33+
34+
start = page * Main.PAGE_SECTION_SIZE;
35+
if (cursor.moveToPosition(start))
36+
{
37+
list = new ArrayList<Section>();
38+
39+
int offset = start + Main.PAGE_SECTION_SIZE;
40+
end = len < offset ? len : offset;
41+
for (int i = start; i < end; i++)
42+
{
43+
Section s = new Section();
44+
String title = cursor.getString(cursor.getColumnIndex("title"));
45+
String url = cursor.getString(cursor.getColumnIndex("url"));
46+
String tableName = cursor.getString(cursor.getColumnIndex("table_name"));
47+
s.setTitle(title);
48+
s.setUrl(url);
49+
s.setTableName(tableName);
50+
list.add(s);
51+
cursor.moveToNext();
52+
}
53+
}
54+
return list;
55+
}
56+
}

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

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.dreamteam.app.commons.SectionHelper;
3939
import com.dreamteam.app.commons.SeriaHelper;
4040
import com.dreamteam.app.commons.UIHelper;
41+
import com.dreamteam.app.dao.SectionDAO;
4142
import com.dreamteam.app.db.DbConstant;
4243
import com.dreamteam.app.db.DbManager;
4344
import com.dreamteam.app.entity.ItemListEntity;
@@ -498,40 +499,42 @@ private void outSectionEdit()
498499

499500
private ArrayList<Section> readSections(int page) throws Exception
500501
{
501-
ArrayList<Section> sections = null;
502-
int len = 0;// 表长
503-
int start = 0;// 其实读
504-
int end = 0;// 结尾
505-
Log.i(tag, "page = " + page);
506-
// 从数据库读数据
507-
DbManager mgr = new DbManager(Main.this, DbManager.DB_NAME, null, 1);
508-
SQLiteDatabase db = mgr.getWritableDatabase();
509-
Cursor cursor = db.query(DbConstant.SECTION_TABLE_NAME,
510-
null, null, null, null, null, null);
511-
len = cursor.getCount();
512-
db.close();
513-
514-
start = page * Main.PAGE_SECTION_SIZE;
515-
if (cursor.moveToPosition(start))
516-
{
517-
sections = new ArrayList<Section>();
518-
519-
int offset = start + Main.PAGE_SECTION_SIZE;
520-
end = len < offset ? len : offset;
521-
for (int i = start; i < end; i++)
522-
{
523-
Section s = new Section();
524-
String title = cursor.getString(cursor.getColumnIndex("title"));
525-
String url = cursor.getString(cursor.getColumnIndex("url"));
526-
String tableName = cursor.getString(cursor.getColumnIndex("table_name"));
527-
s.setTitle(title);
528-
s.setUrl(url);
529-
s.setTableName(tableName);
530-
sections.add(s);
531-
cursor.moveToNext();
532-
}
533-
}
534-
return sections;
502+
SectionDAO sd = new SectionDAO();
503+
return sd.getList(this, page);
504+
// ArrayList<Section> sections = null;
505+
// int len = 0;// 表长
506+
// int start = 0;// 其实读
507+
// int end = 0;// 结尾
508+
// Log.i(tag, "page = " + page);
509+
// // 从数据库读数据
510+
// DbManager mgr = new DbManager(Main.this, DbManager.DB_NAME, null, 1);
511+
// SQLiteDatabase db = mgr.getWritableDatabase();
512+
// Cursor cursor = db.query(DbConstant.SECTION_TABLE_NAME,
513+
// null, null, null, null, null, null);
514+
// len = cursor.getCount();
515+
// db.close();
516+
//
517+
// start = page * Main.PAGE_SECTION_SIZE;
518+
// if (cursor.moveToPosition(start))
519+
// {
520+
// sections = new ArrayList<Section>();
521+
//
522+
// int offset = start + Main.PAGE_SECTION_SIZE;
523+
// end = len < offset ? len : offset;
524+
// for (int i = start; i < end; i++)
525+
// {
526+
// Section s = new Section();
527+
// String title = cursor.getString(cursor.getColumnIndex("title"));
528+
// String url = cursor.getString(cursor.getColumnIndex("url"));
529+
// String tableName = cursor.getString(cursor.getColumnIndex("table_name"));
530+
// s.setTitle(title);
531+
// s.setUrl(url);
532+
// s.setTableName(tableName);
533+
// sections.add(s);
534+
// cursor.moveToNext();
535+
// }
536+
// }
537+
// return sections;
535538
}
536539

537540
@Override

0 commit comments

Comments
 (0)