Skip to content

Commit 094cb23

Browse files
committed
use SectionDAO简化Main UI代码
1 parent 971ffd3 commit 094cb23

File tree

4 files changed

+60
-81
lines changed

4 files changed

+60
-81
lines changed

assets/about.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div style="margin-top:50px;margin-left:20px;font-size:20px">
44
<ul>
55
<li>作者: <font color="blue">zcloud</font>
6-
<li>邮箱: <font color="blue"><u>zcloud00@gmail.com</u></font>
6+
<li>邮箱: <font color="blue"><u>zcloud01@outlook.com</u></font>
77
<li>项目托管地址
88
<ol>
99
<li>oschina: <font color="blue">http://git.oschina.net/zcloud/SimpleReader</font>

src/com/dreamteam/app/dao/SectionDAO.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,23 @@
1212
import com.dreamteam.app.entity.Section;
1313
import com.dreamteam.app.ui.Main;
1414

15+
/**
16+
* @description TODO
17+
* @author zcloud
18+
* @date 2014年9月11日
19+
*/
1520
public class SectionDAO
1621
{
1722
public static final String tag = "SectionDAO";
23+
private Context context;
1824

19-
public ArrayList<Section> getList(Context context, int page)
25+
26+
public SectionDAO(Context context)
27+
{
28+
this.context = context;
29+
}
30+
31+
public ArrayList<Section> getList(int page)
2032
{
2133
ArrayList<Section> list = null;
2234
int len = 0;// 表长
@@ -53,4 +65,40 @@ public ArrayList<Section> getList(Context context, int page)
5365
}
5466
return list;
5567
}
68+
69+
public Section getLast()
70+
{
71+
Section section = new Section();
72+
73+
DbManager mgr = new DbManager(context, DbManager.DB_NAME, null, 1);
74+
SQLiteDatabase db = mgr.getWritableDatabase();
75+
Cursor cursor = db.query(DbConstant.SECTION_TABLE_NAME, null, null, null,
76+
null, null, null);
77+
if (cursor.moveToLast())
78+
{
79+
String title = cursor.getString(cursor.getColumnIndex("title"));
80+
String url = cursor.getString(cursor.getColumnIndex("url"));
81+
String tableName = cursor.getString(cursor
82+
.getColumnIndex("table_name"));
83+
section.setTitle(title);
84+
section.setUrl(url);
85+
section.setTableName(tableName);
86+
}
87+
cursor.close();
88+
db.close();
89+
return section;
90+
}
91+
92+
public int getCount()
93+
{
94+
// 从数据库读数据
95+
DbManager mgr = new DbManager(context, DbManager.DB_NAME, null, 1);
96+
SQLiteDatabase db = mgr.getWritableDatabase();
97+
Cursor cursor = db.query(DbConstant.SECTION_TABLE_NAME,
98+
null, null, null, null, null, null);
99+
int count = cursor.getCount();
100+
cursor.close();
101+
db.close();
102+
return count;
103+
}
56104
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void onClick(View arg0)
3737
Intent intent = new Intent();
3838
intent.setType("message/rfc822");
3939
intent.setAction(Intent.ACTION_SEND);
40-
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"zcloud00@gmail.com"});
40+
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"zcloud01@outlook.com"});
4141
intent.putExtra(Intent.EXTRA_SUBJECT, "用户反馈");
4242
intent.putExtra(Intent.EXTRA_TEXT, msg);
4343
startActivity(Intent.createChooser(intent, "sending mail"));

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

Lines changed: 9 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import android.content.Intent;
99
import android.content.IntentFilter;
1010
import android.content.SharedPreferences.Editor;
11-
import android.database.Cursor;
12-
import android.database.sqlite.SQLiteDatabase;
1311
import android.os.AsyncTask;
1412
import android.os.Bundle;
1513
import android.support.v4.app.FragmentActivity;
@@ -39,8 +37,6 @@
3937
import com.dreamteam.app.commons.SeriaHelper;
4038
import com.dreamteam.app.commons.UIHelper;
4139
import com.dreamteam.app.dao.SectionDAO;
42-
import com.dreamteam.app.db.DbConstant;
43-
import com.dreamteam.app.db.DbManager;
4440
import com.dreamteam.app.entity.ItemListEntity;
4541
import com.dreamteam.app.entity.Section;
4642
import com.dreamteam.app.utils.ImageUtils;
@@ -75,6 +71,7 @@ public class Main extends FragmentActivity
7571
private boolean exit = false;//双击退出
7672
private boolean isEdting = false;//是否编辑section中
7773
private boolean isNight;//是否为夜间模式
74+
private SectionDAO sectionDAO;
7875

7976

8077
@Override
@@ -89,7 +86,7 @@ protected void onCreate(Bundle savedInstanceState)
8986
checkDeprecated();
9087
checkVersion();
9188
}
92-
89+
9390
//检测新版本
9491
public void checkVersion()
9592
{
@@ -135,7 +132,7 @@ public void onReceive(Context context, Intent intent)
135132
} else
136133
{
137134
// 最后一个gridAdapter添加section
138-
lastGridAdapter.addItem(getNewSection());
135+
lastGridAdapter.addItem(sectionDAO.getLast());
139136
}
140137
} else if (action.equals(ACTION_DELETE_SECTION))
141138
{
@@ -193,29 +190,6 @@ public void onReceive(Context context, Intent intent)
193190
registerReceiver(mReceiver, filter);
194191
}
195192

196-
// 获取表新加入的section
197-
private Section getNewSection()
198-
{
199-
Section section = new Section();
200-
DbManager mgr = new DbManager(Main.this, DbManager.DB_NAME, null, 1);
201-
SQLiteDatabase db = mgr.getWritableDatabase();
202-
Cursor cursor = db.query(DbConstant.SECTION_TABLE_NAME, null, null, null,
203-
null, null, null);
204-
if (cursor.moveToLast())
205-
{
206-
String title = cursor.getString(cursor.getColumnIndex("title"));
207-
String url = cursor.getString(cursor.getColumnIndex("url"));
208-
String tableName = cursor.getString(cursor
209-
.getColumnIndex("table_name"));
210-
section.setTitle(title);
211-
section.setUrl(url);
212-
section.setTableName(tableName);
213-
}
214-
cursor.close();
215-
db.close();
216-
return section;
217-
}
218-
219193
private void initPathMenu()
220194
{
221195
PathAnimations.initOffset(this);
@@ -347,8 +321,12 @@ private void openSubscribeCenter()
347321
startActivity(intent);
348322
}
349323

324+
/**
325+
* @description 初始化pagerView,DAO
326+
*/
350327
private void initPager()
351328
{
329+
sectionDAO = new SectionDAO(this);
352330
int pageSize = getPageSize();
353331
for (int i = 0; i < pageSize; i++)
354332
{
@@ -454,7 +432,7 @@ public boolean onItemLongClick(AdapterView<?> parent, View view,
454432
ArrayList<Section> sections = null;
455433
try
456434
{
457-
sections = readSections(currentPage);
435+
sections = sectionDAO.getList(currentPage);
458436
} catch (Exception e)
459437
{
460438
e.printStackTrace();
@@ -497,46 +475,6 @@ private void outSectionEdit()
497475
}
498476
}
499477

500-
private ArrayList<Section> readSections(int page) throws Exception
501-
{
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;
538-
}
539-
540478
@Override
541479
protected void onDestroy()
542480
{
@@ -578,16 +516,9 @@ private void removeLastGridAdapter()
578516
// 从1记
579517
private int getPageSize()
580518
{
581-
// 从数据库读数据
582-
DbManager mgr = new DbManager(Main.this, DbManager.DB_NAME, null, 1);
583-
SQLiteDatabase db = mgr.getWritableDatabase();
584-
Cursor cursor = db.query(DbConstant.SECTION_TABLE_NAME,
585-
null, null, null, null, null, null);
586519
// pager分页
587520
int pageSize = 0;
588-
int sectionCount = cursor.getCount();
589-
cursor.close();
590-
db.close();
521+
int sectionCount = sectionDAO.getCount();
591522

592523
if (sectionCount % PAGE_SECTION_SIZE == 0)
593524
pageSize = sectionCount / PAGE_SECTION_SIZE;

0 commit comments

Comments
 (0)