8
8
import android .content .Intent ;
9
9
import android .content .IntentFilter ;
10
10
import android .content .SharedPreferences .Editor ;
11
- import android .database .Cursor ;
12
- import android .database .sqlite .SQLiteDatabase ;
13
11
import android .os .AsyncTask ;
14
12
import android .os .Bundle ;
15
13
import android .support .v4 .app .FragmentActivity ;
39
37
import com .dreamteam .app .commons .SeriaHelper ;
40
38
import com .dreamteam .app .commons .UIHelper ;
41
39
import com .dreamteam .app .dao .SectionDAO ;
42
- import com .dreamteam .app .db .DbConstant ;
43
- import com .dreamteam .app .db .DbManager ;
44
40
import com .dreamteam .app .entity .ItemListEntity ;
45
41
import com .dreamteam .app .entity .Section ;
46
42
import com .dreamteam .app .utils .ImageUtils ;
@@ -75,6 +71,7 @@ public class Main extends FragmentActivity
75
71
private boolean exit = false ;//双击退出
76
72
private boolean isEdting = false ;//是否编辑section中
77
73
private boolean isNight ;//是否为夜间模式
74
+ private SectionDAO sectionDAO ;
78
75
79
76
80
77
@ Override
@@ -89,7 +86,7 @@ protected void onCreate(Bundle savedInstanceState)
89
86
checkDeprecated ();
90
87
checkVersion ();
91
88
}
92
-
89
+
93
90
//检测新版本
94
91
public void checkVersion ()
95
92
{
@@ -135,7 +132,7 @@ public void onReceive(Context context, Intent intent)
135
132
} else
136
133
{
137
134
// 最后一个gridAdapter添加section
138
- lastGridAdapter .addItem (getNewSection ());
135
+ lastGridAdapter .addItem (sectionDAO . getLast ());
139
136
}
140
137
} else if (action .equals (ACTION_DELETE_SECTION ))
141
138
{
@@ -193,29 +190,6 @@ public void onReceive(Context context, Intent intent)
193
190
registerReceiver (mReceiver , filter );
194
191
}
195
192
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
-
219
193
private void initPathMenu ()
220
194
{
221
195
PathAnimations .initOffset (this );
@@ -347,8 +321,12 @@ private void openSubscribeCenter()
347
321
startActivity (intent );
348
322
}
349
323
324
+ /**
325
+ * @description 初始化pagerView,DAO
326
+ */
350
327
private void initPager ()
351
328
{
329
+ sectionDAO = new SectionDAO (this );
352
330
int pageSize = getPageSize ();
353
331
for (int i = 0 ; i < pageSize ; i ++)
354
332
{
@@ -454,7 +432,7 @@ public boolean onItemLongClick(AdapterView<?> parent, View view,
454
432
ArrayList <Section > sections = null ;
455
433
try
456
434
{
457
- sections = readSections (currentPage );
435
+ sections = sectionDAO . getList (currentPage );
458
436
} catch (Exception e )
459
437
{
460
438
e .printStackTrace ();
@@ -497,46 +475,6 @@ private void outSectionEdit()
497
475
}
498
476
}
499
477
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
-
540
478
@ Override
541
479
protected void onDestroy ()
542
480
{
@@ -578,16 +516,9 @@ private void removeLastGridAdapter()
578
516
// 从1记
579
517
private int getPageSize ()
580
518
{
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 );
586
519
// pager分页
587
520
int pageSize = 0 ;
588
- int sectionCount = cursor .getCount ();
589
- cursor .close ();
590
- db .close ();
521
+ int sectionCount = sectionDAO .getCount ();
591
522
592
523
if (sectionCount % PAGE_SECTION_SIZE == 0 )
593
524
pageSize = sectionCount / PAGE_SECTION_SIZE ;
0 commit comments