-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
2251 lines (1713 loc) · 136 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Shaojie's Blog</title>
<meta name="author" content="Shao jie">
<meta name="description" content="May 5th, 2014 Other 不要做一个浮躁的程序员 1.不要看到别人的回复第一句话就说:给个代码吧!你应该想想为什么。当你自己想出来再参考别人的提示,你就知道自己和别人思路的差异。
2.初学者请不要看太多太多的书那会误人子弟的,先找本系统的学,很多人用了很久都是只对部分功能熟悉而已, …">
<!-- http://t.co/dKP3o1e -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/atom.xml" rel="alternate" title="Shaojie's Blog" type="application/atom+xml">
<link rel="canonical" href="http://www.shaojie.name/">
<link href="/favicon.png" rel="shortcut icon">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
<!--[if lt IE 9]><script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<script src="http://upcdn.b0.upaiyun.com/libs/jquery/jquery-1.7.2.min.js"></script>
<link href='/stylesheets/font.css' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="container">
<div class="left-col">
<div class="intrude-less">
<header id="header" class="inner"><div class="profilepic">
<script src="/javascripts/md5.js"></script>
<script type="text/javascript">
document.write("<img src='/images/avatar.jpg' alt='Profile Picture' style='width: 160px;' />");
</script>
</div>
<h1><a href="/">Shaojie's Blog</a></h1>
<p class="subtitle">专注移动开发Android&IOS&PHP&NODEJS</p>
<nav id="main-nav"><ul class="main">
<li><a href="/">Blog</a></li>
<li><a href="/about">About</a></li>
<li><a href="/blog/archives">Archives</a></li>
</ul>
</nav>
<nav id="sub-nav">
<div class="social">
<a class="weibo" href="http://www.weibo.com/shaojie520" title="Weibo">Weibo</a>
<a class="google" href="https://plus.google.com/+邵杰" rel="author" title="Google+">Google+</a>
<a class="twitter" href="http://twitter.com/shaojie519" title="Twitter">Twitter</a>
<a class="github" href="https://github.com/shaojie519" title="GitHub">GitHub</a>
<a class="dribbble" href="http://www.dribbble.com/shaojie" title="Dribbble">Dribbble</a>
<a class="rss" href="/atom.xml" title="RSS">RSS</a>
</div>
</nav>
</header>
</div>
</div>
<div class="mid-col">
<div class="mid-col-container">
<div id="content" class="inner">
<div itemscope itemtype="http://schema.org/Blog">
<article class="post" itemprop="blogPost" itemscope itemtype="http://schema.org/BlogPosting">
<div class="meta">
<div class="date">
<time datetime="2014-05-05T22:57:51+08:00" data-updated="true" itemprop="datePublished">May 5<span>th</span>, 2014</time></div>
<div class="tags">
<a class='category' href='/blog/categories/other/'>Other</a>
</div>
</div>
<h1 class="title" itemprop="name"><a href="/blog/2014/05/05/chengxuyuan/" itemprop="url">不要做一个浮躁的程序员</a></h1>
<div class="entry-content" itemprop="articleBody">
<ul>
<li><p>1.不要看到别人的回复第一句话就说:给个代码吧!你应该想想为什么。当你自己想出来再参考别人的提示,你就知道自己和别人思路的差异。</p></li>
<li><p>2.初学者请不要看太多太多的书那会误人子弟的,先找本系统的学,很多人用了很久都是只对部分功能熟悉而已,不系统还是不够的。</p></li>
<li><p>3.看帮助,不要因为很难而自己是初学者所以就不看;帮助永远是最好的参考手册,虽然帮助的文字有时候很难看懂,总觉得不够直观。</p></li>
<li><p>4.不要被对象、属性、方法等词汇所迷惑;最根本的是先了解最基础知识。</p></li>
<li><p>5.不要放过任何一个看上去很简单的小问题–他们往往并不那么简单,或者可以引伸出很多知识点;不会举一反三你就永远学不会。</p></li>
<li><p>6.知道一点东西,并不能说明你会写脚本,脚本是需要经验积累的。</p></li>
<li><p>7.学脚本并不难,JSP、ASP、PHP等等也不过如此–难的是长期坚持实践和不遗余力的博览群书;</p></li>
<li><p>8.看再多的书是学不全脚本的,要多实践。</p></li>
<li><p>9.把时髦的技术挂在嘴边,还不如把过时的技术记在心里;</p></li>
<li><p>10.学习脚本最好的方法之一就是多练习;</p></li>
<li><p>11.在任何时刻都不要认为自己手中的书已经足够了;</p></li>
<li><p>12.看得懂的书,请仔细看;看不懂的书,请硬着头皮看;</p></li>
<li><p>13.别指望看第一遍书就能记住和掌握什么——请看第二遍、第三遍;</p></li>
<li><p>14.请把书上的例子亲手到电脑上实践,即使配套光盘中有源文件;</p></li>
<li><p>15.把在书中看到的有意义的例子扩充;并将其切实的运用到自己的工作中;</p></li>
<li><p>16.不要漏掉书中任何一个练习——请全部做完并记录下思路;</p></li>
<li><p>17.当你用脚本到一半却发现自己用的方法很拙劣时,请不要马上停手;请尽快将余下的部分粗略的完成以保证这个代码的完整性,然后分析自己的错误并重新编写和工作。</p></li>
<li><p>18.别心急,写脚本确实不容易;水平是在不断的实践中完善和发展的;</p></li>
<li><p>19.每学到一个脚本难点的时候,尝试着对别人讲解这个知识点并让他理解—-你能讲清楚才说明你真的理解了;</p></li>
<li><p>20.记录下在和别人交流时发现的自己忽视或不理解的知识点;</p></li>
<li><p>21.保存好你做过的所有的源文件—-那是你最好的积累之一;</p></li>
<li><p>22.对于网络,还是希望大家能多利用一下,很多问题不是非要到论坛来问的,首先你要学会自己找答案,比如google、百度都是很好的搜索引擎,你只要输入关键字就能找到很多相关资料,别老是等待别人给你希望,看的出你平时一定也很懒!</p></li>
<li><p>23.到一个论坛,你学会去看以前的帖子,不要什么都不看就发帖子问,也许你的问题早就有人问过了,你再问,别人已经不想再重复了,做为初学者,谁也不希望自己的帖子没人回的。</p></li>
<li><p>24.虽然不是打击初学者,但是这句话还是要说:论坛论坛,就是大家讨论的地方,如果你总期望有高手总无偿指点你,除非他是你亲戚!!讨论者,起码是 水平相当的才有讨论的说法,如果水平真差距太远了,连基本操作都需要别人给解答,谁还跟你讨论呢。能找到很多相关资料,别老是等待别人给你希望,看的出你 平时一定也很懒!</p></li>
</ul>
<p><p></p>
<pre><code>浮躁的人容易问:我到底该学什么;—-别问,学就对了;
浮躁的人容易问:JS有钱途吗;—-建议你去抢银行;
浮躁的人容易说:我要中文版!我英文不行!—-不行?学呀!
浮躁的人分两种:只观望而不学的人;只学而不坚持的人;
浮躁的人永远不是一个高手。
</code></pre>
<p>来自互联网,共勉</p>
</div>
</article>
<article class="post" itemprop="blogPost" itemscope itemtype="http://schema.org/BlogPosting">
<div class="meta">
<div class="date">
<time datetime="2014-05-05T22:44:04+08:00" data-updated="true" itemprop="datePublished">May 5<span>th</span>, 2014</time></div>
<div class="tags">
<a class='category' href='/blog/categories/ios/'>IOS</a>
</div>
</div>
<h1 class="title" itemprop="name"><a href="/blog/2014/05/05/cocoapod/" itemprop="url">Cocoapods安装和使用</a></h1>
<div class="entry-content" itemprop="articleBody">
<h4>CocoaPods是什么</h4>
<p>当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等。可能某个类库又用到其他类库,所以要使用它,必须得另外下载其他类库,而其他类库又用到其他类库,“子子孙孙无穷尽也”,这也许是比较特殊的情况。总之小编的意思就是,手动一个个去下载所需类库十分麻烦。另外一种常见情况是,你项目中用到的类库有更新,你必须得重新下载新版本,重新加入到项目中,十分麻烦。如果能有什么工具能解决这些恼人的问题,那将“善莫大焉”。所以,你需要 CocoaPods。</p>
<p>CocoaPods应该是iOS最常用最有名的类库管理工具了,上述两个烦人的问题,通过cocoaPods,只需要一行命令就可以完全解决,当然前提是你必须正确设置它。重要的是,绝大部分有名的开源类库,都支持CocoaPods。所以,作为iOS程序员的我们,掌握CocoaPods的使用是必不可少的基本技能了</p>
<h4>如何下载和安装CocoaPods</h4>
<p>在安装CocoaPods之前,首先要在本地安装好Ruby环境</p>
<p>假如你在本地已经安装好Ruby环境,那么下载和安装CocoaPods将十分简单,只需要一行命令。在Terminator(也就是终端)中输入以下命令</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>sudo gem install cocoapods
</span><span class='line'>pod setup</span></code></pre></td></tr></table></div></figure>
<p>上面第二行执行时,会输出Setting up CocoaPods master repo,但是会等待比较久的时间。这步其实是Cocoapods在将它的信息下载到 ~/.cocoapods目录下,如果你等太久,可以试着cd到那个目录,用du -sh *来查看下载进度</p>
<p>如果你在终端中敲入这个命令之后,会发现半天没有任何反应,可以更新一下ruby的源:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>gem sources --remove https://rubygems.org/
</span><span class='line'>gem sources -a http://ruby.taobao.org/
</span><span class='line'>gem sources -l</span></code></pre></td></tr></table></div></figure>
<h4>如何使用CocoaPod</h4>
<ol>
<li>利用CocoaPods,在项目中导入AFNetworking类库
AFNetworking类库在GitHub地址是:<a href="https://github.com/AFNetworking/AFNetworking">https://github.com/AFNetworking/AFNetworking</a>
为了确定AFNetworking是否支持CocoaPods,可以用CocoaPods的搜索功能验证一下。在终端中输入:</li>
</ol>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>pod search AFNetWorking</span></code></pre></td></tr></table></div></figure>
<p>过几秒钟之后,你会在终端中看到关于AFNetworking类库的一些信息</p>
<p>“
–> AFNetworking (2.2.1)
A delightful iOS and OS X networking framework.
pod ‘AFNetworking’, ‘~> 2.2.1’
– Homepage: <a href="https://github.com/AFNetworking/AFNetworking">https://github.com/AFNetworking/AFNetworking</a>
– Source: <a href="https://github.com/AFNetworking/AFNetworking.git">https://github.com/AFNetworking/AFNetworking.git</a>
– Versions: 2.2.1, 2.2.0, 2.1.0, 2.0.3, 2.0.2, 2.0.1, 2.0.0-RC3, 2.0.0-RC2, 2.0.0-RC1, 2.0.0, 1.3.3, 1.3.2, 1.3.1, 1.3.0, 1.2.1, 1.2.0, 1.1.0, 1.0RC3, 1.0RC2, 1.0RC1, 1.0.1, 1.0, 0.9.2, 0.9.1, 0.9.0, 0.7.0, 0.5.1, 0.10.1, 0.10.0 [master repo]
– Sub specs:</p>
<pre><code> - AFNetworking/Serialization (2.2.1)
- AFNetworking/Security (2.2.1)
- AFNetworking/Reachability (2.2.1)
- AFNetworking/NSURLConnection (2.2.1)
- AFNetworking/NSURLSession (2.2.1)
- AFNetworking/UIKit (2.2.1)
</code></pre>
<p>“
这说明,AFNetworking是支持CocoaPods,所以我们可以利用CocoaPods将AFNetworking导入你的项目中。
先利用Xcode创建一个项目,在项目的目录下新建一个名为Podfile的文件</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>platform :ios, '7.0'
</span><span class='line'>pod "AFNetworking", "~> 2.2.0"</span></code></pre></td></tr></table></div></figure>
<p>这两句文字的意思是,当前AFNetworking支持的iOS最高版本是iOS 7.0, 要下载的AFNetworking版本是2.2.0。</p>
<p>这时候,你就可以利用CocoPods下载AFNetworking类库了。还是在终端中的当前项目目录下,运行以下命令:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>pod install</span></code></pre></td></tr></table></div></figure>
<p>运行完成后,使用CocoaPods生成的 .xcworkspace 文件来打开工程,而不是以前的 .xcodeproj 文件
每次更改了Podfile文件,你需要重新执行一次pod install命令</p>
<h4>.gitignore</h4>
<p>当你执行pod install之后,除了Podfile外,cocoapods还会生成一个名为Podfile.lock的文件,你不应该把这个文件加入到.gitignore中。因为Podfile.lock会锁定当前各依赖库的版本,之后如果多次执行pod install 不会更改版本,要pod update才会改Podfile.lock了。这样多人协作的时候,可以防止第三方库升级把程序搞挂</p>
</div>
</article>
<article class="post" itemprop="blogPost" itemscope itemtype="http://schema.org/BlogPosting">
<div class="meta">
<div class="date">
<time datetime="2014-05-01T00:20:18+08:00" data-updated="true" itemprop="datePublished">May 1<span>st</span>, 2014</time></div>
<div class="tags">
<a class='category' href='/blog/categories/ios/'>IOS</a>
</div>
</div>
<h1 class="title" itemprop="name"><a href="/blog/2014/05/01/ios-sqlite/" itemprop="url">Ios Sqlite3使用</a></h1>
<div class="entry-content" itemprop="articleBody">
<h5>iOS中使用</h5>
<p>在 iOS 中 sqlite3 库是一套纯 C 的接口,因此很方便地就可以在 obj-c 源码中无痕使用它
首先,需要在Frameworks中加入所需的库Library libsqlite3.0.dylib</p>
<p>这样可以导入头文件了</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>#import "ViewController.h"
</span><span class='line'>#import "sqlite3.h"
</span><span class='line'>#define kDatabaseName @"city.db"
</span><span class='line'>@interface ViewController (){
</span><span class='line'> sqlite3 * database;
</span><span class='line'>}
</span><span class='line'>@end</span></code></pre></td></tr></table></div></figure>
<h4>初始化数据库</h4>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>-(BOOL)initializeDb {
</span><span class='line'> NSLog(@"initializeDB");
</span><span class='line'> // look to see if DB is in known location (~/Documents/$DATABASE_FILE_NAME)
</span><span class='line'> //START:code.DatabaseShoppingList.findDocumentsDirectory
</span><span class='line'> NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
</span><span class='line'> NSString *documentFolderPath = [searchPaths objectAtIndex:0];
</span><span class='line'> //查看文件目录
</span><span class='line'> NSLog(@"%@", documentFolderPath);
</span><span class='line'> self.databaseFilePath = [documentFolderPath stringByAppendingPathComponent:@"city.db"];
</span><span class='line'>
</span><span class='line'> if(![[NSFileManager defaultManager] fileExistsAtPath:self.databaseFilePath]){
</span><span class='line'> //didnit find db, need to copy
</span><span class='line'> NSString *backupDbPath = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"db"];
</span><span class='line'>
</span><span class='line'> if(backupDbPath == nil) {
</span><span class='line'> //couldn't find backup db to copy
</span><span class='line'> return NO;
</span><span class='line'> }else {
</span><span class='line'> BOOL copiedBackupDb = [[NSFileManager defaultManager] copyItemAtPath:backupDbPath toPath:self.databaseFilePath error:nil];
</span><span class='line'> if(!copiedBackupDb){
</span><span class='line'> return NO;
</span><span class='line'> }
</span><span class='line'> }
</span><span class='line'>
</span><span class='line'> }
</span><span class='line'> return TRUE;
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>
<h4>创建表</h4>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>-(BOOL) createTable{
</span><span class='line'> char *sql = "CREATE TABLE city (id integer primary key, cid integer, cityName text)";
</span><span class='line'> sqlite3_stmt *statment;
</span><span class='line'> char *errorMsg;
</span><span class='line'> if(sqlite3_exec(database, sql, NULL, NULL, &errorMsg) != SQLITE_OK){
</span><span class='line'> NSLog(@"Error: create table failed %s", sqlite3_errmsg(database));
</span><span class='line'> return NO;
</span><span class='line'> }
</span><span class='line'>
</span><span class='line'> int success = sqlite3_finalize(statment);
</span><span class='line'> if(success != SQLITE_DONE){
</span><span class='line'> NSLog(@"failed to dehydate: %s", sqlite3_errmsg(database));
</span><span class='line'> return NO;
</span><span class='line'> }
</span><span class='line'> return YES;
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>
<h4>向表中插入记录</h4>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>-(BOOL) insertOne{
</span><span class='line'> char *errorMsg;
</span><span class='line'> const char *sql = "insert into city (cid, cityName) values(101, '上海')";
</span><span class='line'> if(sqlite3_exec(database, sql, NULL, NULL, &errorMsg) == SQLITE_OK){
</span><span class='line'> NSLog(@"Insert ok");
</span><span class='line'> return YES;
</span><span class='line'> }else{
</span><span class='line'> NSLog(@"error: %s",errorMsg);
</span><span class='line'> sqlite3_free(errorMsg);
</span><span class='line'> return NO;
</span><span class='line'> }
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>
<h4>查询数据库</h4>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>-(void) showCitys{
</span><span class='line'> const char * sql = "select * from city";
</span><span class='line'> sqlite3_stmt *statment;
</span><span class='line'> if(sqlite3_prepare_v2(database, sql, -1, &statment, nil) == SQLITE_OK) {
</span><span class='line'> NSLog(@"select ok.");
</span><span class='line'> }
</span><span class='line'> while(sqlite3_step(statment) == SQLITE_ROW) {
</span><span class='line'> int id = sqlite3_column_int(statment, 1);
</span><span class='line'> //char *name = (char *) sqlite3_column_text(statment, 2);
</span><span class='line'> //直接用的char类型来中文会有乱码,使用NSString代替
</span><span class='line'> NSString *name=[[NSString alloc] initWithCString:(char *)sqlite3_column_text(statment , 2) encoding:NSUTF8StringEncoding];
</span><span class='line'> NSLog(@"row >>id %i, name %@", id, name);
</span><span class='line'> }
</span><span class='line'>
</span><span class='line'> sqlite3_finalize(statment);
</span><span class='line'>
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>
<h4>关闭数据库</h4>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>- (void)viewDidUnload {
</span><span class='line'>
</span><span class='line'> sqlite3_close(database);
</span><span class='line'>
</span><span class='line'>} </span></code></pre></td></tr></table></div></figure>
</div>
</article>
<article class="post" itemprop="blogPost" itemscope itemtype="http://schema.org/BlogPosting">
<div class="meta">
<div class="date">
<time datetime="2014-03-24T23:06:01+08:00" data-updated="true" itemprop="datePublished">Mar 24<span>th</span>, 2014</time></div>
<div class="tags">
<a class='category' href='/blog/categories/android/'>Android</a>
</div>
</div>
<h1 class="title" itemprop="name"><a href="/blog/2014/03/24/android-mvp/" itemprop="url">Android MVP 模式</a></h1>
<div class="entry-content" itemprop="articleBody">
<h4>MVP模式与MVC模式</h4>
<p>MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数据,View负责显示。作为一种新的模式,MVP与MVC有着一个重大的区别:在MVP中View并不直接使用Model,它们之间的通信是通过Presenter (MVC中的Controller)来进行的,所有的交互都发生在Presenter内部,而在MVC中View会从直接Model中读取数据而不是通过 Controller
<img src="http://img.hb.aicdn.com/c098e0b36a1be7bb989ca451e2f084d5758eb88b4e76-x3xslP_fw658" alt="" /></p>
<h4>什么是MVC(Model View Presenter)模式</h4>
<ol>
<li>为了使得视图接口可以与模型和控制器进行交互,控制器执行一些初始化事件</li>
<li>用户通过视图(用户接口)执行一些操作</li>
<li>控制器处理用户行为(可以用观察着模式实现)并通知模型进行更新</li>
<li>模型引发一些事件,以便将改变发告知视图</li>
<li>视图处理模型变更的事件,然后显示新的模型数据</li>
<li>用户接口等待用户的进一步操作</li>
</ol>
<h4>Example</h4>
<p>首先,我们先申明View的接口,有2个简单的方法</p>
<figure class='code'><figcaption><span>viewInterface </span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kd">public</span> <span class="n">inteface</span> <span class="n">IResultsView</span> <span class="o">{</span>
</span><span class='line'> <span class="c1">//set title</span>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">showTitle</span><span class="o">(</span><span class="n">String</span> <span class="n">title</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">//set result text</span>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">showResults</span><span class="o">(</span><span class="n">String</span> <span class="n">results</span><span class="o">);</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>这些方法是说明我希望View中视图的变化,这里不关心它怎么发生的</p>
<p>然后再定义一个Presenter的接口</p>
<figure class='code'><figcaption><span>presenterInteface</span><a href='http://www.shaojie.name'>link</a></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kd">public</span> <span class="kd">interface</span> <span class="nc">IResultsPresenter</span><span class="o">{</span>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onResume</span><span class="o">(</span><span class="n">Context</span> <span class="n">context</span><span class="o">);</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>在视图里(Activity/Fragemnt)里onResume()触发的时候,调用Presenter。</p>
<figure class='code'><figcaption><span>presenterInteface</span><a href='http://www.shaojie.name'>link</a></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="n">java</span> <span class="n">ResultsPreseter</span>
</span><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">ResultsPresenter</span> <span class="kd">implements</span> <span class="n">IResultsPresenter</span> <span class="o">{</span>
</span><span class='line'> <span class="kd">private</span> <span class="n">IResultsView</span> <span class="n">resultsView</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="nf">ResultsPresenter</span><span class="o">(</span><span class="n">IResultsView</span> <span class="n">resultsView</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="k">this</span><span class="o">.</span><span class="na">resultsView</span> <span class="o">=</span> <span class="n">resultsView</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="nd">@Override</span>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onResume</span><span class="o">(</span><span class="n">Context</span> <span class="n">context</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// Get a title</span>
</span><span class='line'> <span class="n">getTitle</span><span class="o">(</span><span class="n">context</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'> <span class="c1">// Get results</span>
</span><span class='line'> <span class="n">getResults</span><span class="o">(</span><span class="n">context</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>这里,会触发IResultView里定义的2个函数</p>
<figure class='code'><figcaption><span>presenterInteface</span><a href='http://www.shaojie.name'>link</a></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">FragmentResults</span> <span class="kd">extends</span> <span class="n">Fragment</span> <span class="kd">implements</span> <span class="n">IResultsView</span> <span class="o">{</span>
</span><span class='line'> <span class="kd">private</span> <span class="n">View</span> <span class="n">view</span><span class="o">;</span>
</span><span class='line'> <span class="kd">private</span> <span class="n">TextView</span> <span class="n">title</span><span class="o">,</span> <span class="n">content</span><span class="o">;</span>
</span><span class='line'> <span class="kd">private</span> <span class="n">IResultsPresenter</span> <span class="n">presenter</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="nf">FragmentResults</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'> <span class="c1">// Required empty public constructor</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'> <span class="nd">@Override</span>
</span><span class='line'> <span class="kd">public</span> <span class="n">View</span> <span class="nf">onCreateView</span><span class="o">(</span><span class="n">LayoutInflater</span> <span class="n">inflater</span><span class="o">,</span> <span class="n">ViewGroup</span> <span class="n">container</span><span class="o">,</span>
</span><span class='line'> <span class="n">Bundle</span> <span class="n">savedInstanceState</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">view</span> <span class="o">=</span> <span class="n">inflater</span><span class="o">.</span><span class="na">inflate</span><span class="o">(</span><span class="n">R</span><span class="o">.</span><span class="na">layout</span><span class="o">.</span><span class="na">fragment_results</span><span class="o">,</span> <span class="kc">null</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="n">view</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="nd">@Override</span>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onViewCreated</span><span class="o">(</span><span class="n">View</span> <span class="n">view</span><span class="o">,</span> <span class="n">Bundle</span> <span class="n">savedInstanceState</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="kd">super</span><span class="o">.</span><span class="na">onViewCreated</span><span class="o">(</span><span class="n">view</span><span class="o">,</span> <span class="n">savedInstanceState</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="n">presenter</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ResultsPresenter</span><span class="o">(</span><span class="k">this</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="n">title</span> <span class="o">=</span> <span class="o">(</span><span class="n">TextView</span><span class="o">)</span> <span class="n">view</span><span class="o">.</span><span class="na">findViewById</span><span class="o">(</span><span class="n">R</span><span class="o">.</span><span class="na">id</span><span class="o">.</span><span class="na">results_title</span><span class="o">);</span>
</span><span class='line'> <span class="n">content</span> <span class="o">=</span> <span class="o">(</span><span class="n">TextView</span><span class="o">)</span> <span class="n">view</span><span class="o">.</span><span class="na">findViewById</span><span class="o">(</span><span class="n">R</span><span class="o">.</span><span class="na">id</span><span class="o">.</span><span class="na">results_text</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="nd">@Override</span>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onResume</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'> <span class="kd">super</span><span class="o">.</span><span class="na">onResume</span><span class="o">();</span>
</span><span class='line'>
</span><span class='line'> <span class="n">presenter</span><span class="o">.</span><span class="na">onResume</span><span class="o">(</span><span class="n">getActivity</span><span class="o">());</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="nd">@Override</span>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">showTitle</span><span class="o">(</span><span class="n">String</span> <span class="n">title</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="k">this</span><span class="o">.</span><span class="na">title</span><span class="o">.</span><span class="na">setText</span><span class="o">(</span><span class="n">title</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="nd">@Override</span>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">showResults</span><span class="o">(</span><span class="n">String</span> <span class="n">results</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="n">content</span><span class="o">.</span><span class="na">setText</span><span class="o">(</span><span class="n">results</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>很明显,我们就可以脱离用户接口来测试这些逻辑</p>
<figure class='code'><figcaption><span>presenterInteface</span><a href='http://www.shaojie.name'>link</a></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">TestResultsPresenter</span> <span class="kd">extends</span> <span class="n">AndroidTestCase</span> <span class="o">{</span>
</span><span class='line'> <span class="kd">private</span> <span class="n">IResultsPresenter</span> <span class="n">resultsPresenter</span><span class="o">;</span>
</span><span class='line'> <span class="kd">private</span> <span class="n">String</span> <span class="n">resultTitle</span><span class="o">;</span>
</span><span class='line'> <span class="kd">private</span> <span class="n">String</span> <span class="n">resultData</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="nf">TestResultsPresenter</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'> <span class="kd">super</span><span class="o">();</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="nd">@Override</span>
</span><span class='line'> <span class="kd">protected</span> <span class="kt">void</span> <span class="nf">setUp</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
</span><span class='line'> <span class="kd">super</span><span class="o">.</span><span class="na">setUp</span><span class="o">();</span>
</span><span class='line'>
</span><span class='line'> <span class="n">IResultsView</span> <span class="n">resultsView</span> <span class="o">=</span> <span class="k">new</span> <span class="n">TestResultsView</span><span class="o">();</span>
</span><span class='line'> <span class="n">resultsPresenter</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ResultsPresenter</span><span class="o">(</span><span class="n">resultsView</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="nd">@Override</span>
</span><span class='line'> <span class="kd">protected</span> <span class="kt">void</span> <span class="nf">tearDown</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
</span><span class='line'> <span class="kd">super</span><span class="o">.</span><span class="na">tearDown</span><span class="o">();</span>
</span><span class='line'> <span class="n">resultsPresenter</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">testResultFormat</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'> <span class="n">Utilities</span><span class="o">.</span><span class="na">SmsResultManager</span> <span class="n">smsResultManager</span> <span class="o">=</span> <span class="n">Utilities</span><span class="o">.</span><span class="na">SmsResultManager</span><span class="o">.</span><span class="na">getInstance</span><span class="o">(</span><span class="n">getContext</span><span class="o">());</span>
</span><span class='line'> <span class="n">smsResultManager</span><span class="o">.</span><span class="na">removeAllSmsResults</span><span class="o">();</span>
</span><span class='line'> <span class="n">smsResultManager</span><span class="o">.</span><span class="na">saveResult</span><span class="o">(</span><span class="s">"foo"</span><span class="o">,</span> <span class="k">new</span> <span class="n">SmsResult</span><span class="o">(</span><span class="s">"ark"</span><span class="o">,</span> <span class="s">"foo"</span><span class="o">));</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// Build expected Content</span>
</span><span class='line'> <span class="n">String</span> <span class="n">expectedContent</span> <span class="o">=</span> <span class="s">"ark\t\tfoo\t\tUnknown\n"</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'> <span class="n">resultsPresenter</span><span class="o">.</span><span class="na">onResume</span><span class="o">(</span><span class="n">getContext</span><span class="o">());</span>
</span><span class='line'>
</span><span class='line'> <span class="n">assertEquals</span><span class="o">(</span><span class="n">expectedContent</span><span class="o">,</span> <span class="n">resultData</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">testResultTitle</span><span class="o">(){</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// put something</span>
</span><span class='line'> <span class="n">Date</span> <span class="n">nowDate</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Date</span><span class="o">();</span>
</span><span class='line'> <span class="n">DateFormat</span> <span class="n">df</span> <span class="o">=</span> <span class="n">DateFormat</span><span class="o">.</span><span class="na">getDateTimeInstance</span><span class="o">(</span>
</span><span class='line'> <span class="n">DateFormat</span><span class="o">.</span><span class="na">SHORT</span><span class="o">,</span>
</span><span class='line'> <span class="n">DateFormat</span><span class="o">.</span><span class="na">SHORT</span><span class="o">,</span>
</span><span class='line'> <span class="n">Locale</span><span class="o">.</span><span class="na">getDefault</span><span class="o">());</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// Set date time in prefs</span>
</span><span class='line'> <span class="n">Preferences</span><span class="o">.</span><span class="na">writePreferenceValue</span><span class="o">(</span><span class="n">getContext</span><span class="o">(),</span>
</span><span class='line'> <span class="n">getContext</span><span class="o">().</span><span class="na">getString</span><span class="o">(</span><span class="n">R</span><span class="o">.</span><span class="na">string</span><span class="o">.</span><span class="na">pref_emergency_date_time_key</span><span class="o">),</span> <span class="n">nowDate</span><span class="o">.</span><span class="na">getTime</span><span class="o">());</span>
</span><span class='line'>
</span><span class='line'> <span class="n">String</span> <span class="n">formattedDateTime</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="na">format</span><span class="o">(</span><span class="n">nowDate</span><span class="o">);</span>
</span><span class='line'> <span class="n">String</span> <span class="n">expectedTitle</span> <span class="o">=</span> <span class="n">String</span><span class="o">.</span><span class="na">format</span><span class="o">(</span><span class="n">getContext</span><span class="o">().</span><span class="na">getString</span><span class="o">(</span><span class="n">R</span><span class="o">.</span><span class="na">string</span><span class="o">.</span><span class="na">results_title</span><span class="o">),</span>
</span><span class='line'> <span class="n">formattedDateTime</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// Trigger onResume event in Results Presenter</span>
</span><span class='line'> <span class="n">resultsPresenter</span><span class="o">.</span><span class="na">onResume</span><span class="o">(</span><span class="n">getContext</span><span class="o">());</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// Test not null</span>
</span><span class='line'> <span class="n">assertNotNull</span><span class="o">(</span><span class="n">resultTitle</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">// Test as expected</span>
</span><span class='line'> <span class="n">assertEquals</span><span class="o">(</span><span class="n">expectedTitle</span><span class="o">,</span> <span class="n">resultTitle</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'> <span class="kd">private</span> <span class="kd">class</span> <span class="nc">TestResultsView</span> <span class="kd">implements</span> <span class="n">IResultsView</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="nd">@Override</span>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">showTitle</span><span class="o">(</span><span class="n">String</span> <span class="n">title</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">resultTitle</span> <span class="o">=</span> <span class="n">title</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="nd">@Override</span>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">showResults</span><span class="o">(</span><span class="n">String</span> <span class="n">results</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">resultData</span> <span class="o">=</span> <span class="n">results</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
</div>
</article>
<article class="post" itemprop="blogPost" itemscope itemtype="http://schema.org/BlogPosting">
<div class="meta">
<div class="date">
<time datetime="2014-03-24T23:05:55+08:00" data-updated="true" itemprop="datePublished">Mar 24<span>th</span>, 2014</time></div>
<div class="tags">
<a class='category' href='/blog/categories/other/'>Other</a>
</div>
</div>
<h1 class="title" itemprop="name"><a href="/blog/2014/03/24/4/" itemprop="url">十四个很准的心里暗示</a></h1>
<div class="entry-content" itemprop="articleBody">
<p>1、当你对某件事情抱着百分之一万的相信,它最后就会变成事实。</p>
<p>2、期望定律 期望定律告诉我们,当我们怀着对某件事情非常强烈期望的时候,我们所期望的事物就会出现。</p>
<p>3、情绪定律 情绪定律告诉我们,人百分之百是情绪化的。即使有人说某人很理性,其实当这个人很有”理性”地思考问题的时候,也是受到他当时情绪状态的影响,”理性地思考”本身也是一种情绪状态。所以人百分之百是情绪化的动物,而且任何时候的决定都是情绪化的决定。</p>
<p>4、因果定律 任何事情的发生,都有其必然的原因。有因才有果。换句话说,当你看到任何现象的时候,你不用觉得不可理解或者奇怪,因为任何事情的发生都必有其原因。你今天的现状结果是你过去种下的因导致的结果。</p>
<p>5、吸引定律 当你的思想专注在某一领域的时候,跟这个领域相关的人、事、物就会被你吸引而来。</p>
<p> 6、重复定律 任何的行为和思维,只要你不断的重复就会得到不断的加强。在你的潜意识当中,只要你能够不断地重复一些人、事、物,它们都会在潜意识里变成事实。</p>
<p> 7、累积定律 很多年轻人都曾梦想做一番大事业,其实天下并没有什么大事可做,有的只是小事。一件一件小事累积起来就形成了大事。任何大成就或者大灾难都是累积的结果。</p>
<p>8、辐射定律 当你做一件事情的时候,影响的并不只是这件事情的本身,它还会辐射到相关的其他领域。任何事情都有辐射作用。</p>
<p>9、相关定律 相关定律告诉我们:这个世界上的每一件事情之间都有一定的联系,没有一件事情是完全独立的。要解决某个难题最好从其他相关的某个地方人手,而不只是专注在一个困难点上。</p>
<p> 10、专精定律 专精定律告诉我们,只有专精在一个领域,这个领域才能有所发展。所以无论你做任何的行业都要把做该行业的最顶尖为目标,只有当你能够专精的时候,你所做的领域才会出类拔萃地成长。</p>
<p> 11、替换定律 替换定律就是说,当我们有一项不想要的记忆或者是负面的习惯,我们是无法完全去除掉,只能用一种新的记忆或新的习惯去替换他。</p>
<p> 12、惯性定律 任何事情只要你能够持续不断去加强它,它终究会变成一种习惯。</p>
<p>13、显现定律 显现定律就是说,当我们持续寻找、追问答案的时候,它们最终都必将显现。</p>
<p> 14、需求定律 任何人做任何事情都是带有一种需求。尊重并满足对方的需求,别人才会尊重我们的需求</p>
</div>
</article>
<article class="post" itemprop="blogPost" itemscope itemtype="http://schema.org/BlogPosting">
<div class="meta">
<div class="date">
<time datetime="2014-03-24T23:05:52+08:00" data-updated="true" itemprop="datePublished">Mar 24<span>th</span>, 2014</time></div>
<div class="tags">
<a class='category' href='/blog/categories/android/'>Android</a>
</div>
</div>
<h1 class="title" itemprop="name"><a href="/blog/2014/03/24/adb-commands-for-android/" itemprop="url">Handy Adb Commands for Android</a></h1>
<div class="entry-content" itemprop="articleBody">
<h4>查看连接的设备</h4>
<p>使用这个命令查看当前连接的所有设备和它们的ID</p>
<p><code>adb devices</code></p>
<p>如果你有多个设备连接,使用<code>adb -s DEVICE_ID</code>选中你需要操作的设备</p>
<h4>安装应用</h4>