传送门:Android+Sqlite 实现古诗阅读应用(一)

  Hi,又回来了,最近接到很多热情洋溢的小伙伴们的来信,吼开心哈,我会继续努力的=-=!

  上回的东西我们做到了有个textview能随机选择诗来进行显示,这也是我做这个东西的初衷,我想找我到底有哪些古诗没有读过,更想感受一下风吹哪页看哪页的闲适(扯远了=-=!),所以功能现在差不多算是结束了,

不过一个古诗应用这么丑可不行,还有就是,我找到了我要的诗我也得能收藏啊,要是下次忘了可怎么办啊,所以这里面还有一些知识点,我们能从接下来的功能中学到:

1.再做一个启动界面:

  打开数据库,随着数据库的增大会有一点卡顿,我们加个界面来过渡缓解一下:

 1 package com.lfk.poem;
 2 import android.app.Activity;
 3 import android.content.Intent;
 4 import android.os.Bundle;
 5 import android.util.Log;
 6 import android.view.View;
 7 import android.view.animation.AlphaAnimation;
 8 import android.view.animation.Animation;
 9 
10 /**
11  * Created by Administrator on 2015/4/11.
12  */
13 public class Opening extends Activity {
14     @Override
15     public void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         final View view = View.inflate(this, R.layout.activity_opening, null);
18         setContentView(view);
19         //渐变展示启动屏
20         AlphaAnimation start = new AlphaAnimation(0.3f,1.0f);
21         start.setDuration(2000);
22         view.startAnimation(start);
23         start.setAnimationListener(new Animation.AnimationListener()
24         {
25             @Override
26             public void onAnimationEnd(Animation arg0) {
27                 Log.e("linc", "---start!");
28                 try{
29                     Intent intent = new Intent();
30                     intent.setClass(Opening.this,MainActivity.class);
31                     Opening.this.startActivity(intent);
32                     Opening.this.finish();
33                 }
34                 catch(Exception e)
35                 {
36                     e.printStackTrace();
37                 }
38             }
39             @Override
40             public void onAnimationRepeat(Animation animation) {}
41             @Override
42             public void onAnimationStart(Animation animation) {}
43         });
44 
45 
46     }
47 }

 

这是做过的样子:

    Android+Sqlite 实现古诗阅读应用(二)

2.修改Actionbar为透明的叠加模式:

  这个我在之前的博客里已经写过了,可以参考一下即时通讯的第五篇:http://www.cnblogs.com/lfk-dsk/p/4419418.html

3.数据库的导入:

  上次为了测试我们只导入了5首古诗作为测试,这回用正则表达式调整了格式,导入了唐诗三百首。

  Android+Sqlite 实现古诗阅读应用(二)

将txt做成了这种格式,然后倒入数据库管理软件。

  Android+Sqlite 实现古诗阅读应用(二)

数据库里的格式就是这样的了,然后替换数据库就好了,想要现成的可找我要。

4.背景和刷新:

  自然不用说添加自己喜欢的古风背景就好。

  刷新我不用Button了,改用google的下拉刷新,我在这个博文里写过:http://www.cnblogs.com/lfk-dsk/p/4433319.html

  每次刷新一下就会重新找一首诗。

5.主活动的修改

  1 package com.lfk.poem;
  2 
  3 import android.app.Activity;
  4 import android.content.Intent;
  5 import android.database.Cursor;
  6 import android.database.sqlite.SQLiteDatabase;
  7 import android.graphics.Typeface;
  8 import android.os.Bundle;
  9 import android.os.Environment;
 10 import android.os.Handler;
 11 import android.support.v4.widget.SwipeRefreshLayout;
 12 import android.util.Log;
 13 import android.view.Menu;
 14 import android.view.MenuInflater;
 15 import android.view.MenuItem;
 16 import android.widget.RelativeLayout;
 17 import android.widget.TextView;
 18 import android.widget.Toast;
 19 
 20 import java.io.File;
 21 import java.io.FileNotFoundException;
 22 import java.io.FileOutputStream;
 23 import java.io.IOException;
 24 import java.io.InputStream;
 25 
 26 
 27 public class MainActivity extends Activity {
 28     private  final int BUFFER_SIZE = 400000;
 29     public static final String DB_NAME = "poem_all.db"; //保存的数据库文件名
 30     public static final String DB_USER_NAME = "poem_user.db";
 31     public static final String PACKAGE_NAME = "com.lfk.poem";// 应用的包名
 32     public static final String DB_PATH = "/data"
 33             + Environment.getDataDirectory().getAbsolutePath() +"/"
 34             + PACKAGE_NAME+ "/databases"; // 在手机里存放数据库的位置
 35     private SwipeRefreshLayout swipeLayout;
 36     private RelativeLayout main_layout;
 37     private TextView textView;
 38     private static int ID = 0;
 39     private String NAME;
 40     private String POEM;
 41     @Override
 42     protected void onCreate(Bundle savedInstanceState) {
 43         super.onCreate(savedInstanceState);
 44         setContentView(R.layout.activity_main);
 45 
 46         Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/font_ksj.ttf");
 47         textView = (TextView)findViewById(R.id.text_view);
 48         textView.setTypeface(typeface);
 49 
 50         main_layout = (RelativeLayout)findViewById(R.id.main_layout);
 51         ChangeBackground();
 52         FindaPoem();
 53         swipeLayout = (SwipeRefreshLayout) this.findViewById(R.id.swipe_refresh);
 54         swipeLayout.setColorScheme(R.color.haah);
 55         swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
 56             @Override
 57             public void onRefresh() {
 58                 new Handler().postDelayed(new Runnable() {//延迟跳转=-=
 59                     public void run() {
 60                         swipeLayout.setRefreshing(true);
 61                         FindaPoem();
 62                         swipeLayout.setRefreshing(false);
 63                     }
 64                 }, 500);
 65             }
 66         });
 67     }
 68     private void FindaPoem() {
 69         int ll = (int) (1 + Math.random() * (59170));
 70         ID = ll;
 71         SQLiteDatabase database = openDatabase();
 72         Cursor cursor = database.rawQuery("Select * From poem Where _id = " + ll, null);
 73         cursor.moveToFirst();
 74         String poem = cursor.getString(1)+"\n"+"\n"+cursor.getString(2)+"\n"+"\n"+cursor.getString(13);
 75         NAME = cursor.getString(2)+": "+cursor.getString(1);
 76         POEM = cursor.getString(13);
 77         Log.e(poem, "================");
 78         textView.setText(poem);
 79         cursor.close();
 80         database.close();
 81     }
 82     private void ChangeBackground(){
 83         int ln = (int) (1 + Math.random() * (5));
 84         switch (ln){
 85             case 1:
 86                 main_layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.detail_bg));
 87                 break;
 88             case 2:
 89                 main_layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.navigation_1));
 90                 break;
 91             case 3:
 92                 main_layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.navigation_2));
 93                 break;
 94             case 4:
 95                 main_layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.navigation_3));
 96                 break;
 97             case 5:
 98                 main_layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.navigation_4));
 99                 break;
100         }
101     }
102     public SQLiteDatabase openDatabase() {
103         try {
104             File myDataPath = new File(DB_PATH);
105             if (!myDataPath.exists())
106             {
107                 myDataPath.mkdirs();// 如果没有这个目录,则创建
108             }
109             String dbfile = myDataPath+"/"+DB_NAME;
110             if (!(new File(dbfile).exists())) {// 判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库
111                 InputStream is;
112                 is = this.getResources().openRawResource(R.raw.poem_all); // 欲导入的数据库
113                 FileOutputStream fos = new FileOutputStream(dbfile);
114                 byte[] buffer = new byte[BUFFER_SIZE];
115                 int count = 0 ;
116                 while ((count = is.read(buffer)) > 0) {
117                         fos.write(buffer, 0, count);
118                 }
119                 fos.close();
120                 is.close();
121             }
122             SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
123             Log.e("=======================","get it  ======================");
124             return db;
125         } catch (FileNotFoundException e) {
126             Log.e("Database", "File not found");
127             e.printStackTrace();
128         } catch (IOException e) {
129             Log.e("Database", "IO exception");
130             e.printStackTrace();
131         }
132         return null;
133     }
134     void AddaPoemToCollect(){
135         File myDataPath = new File(DB_PATH);
136         String dbfile = myDataPath+"/"+DB_USER_NAME;
137         SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
138         //ContentValues contentValues = new ContentValues();
139         db.execSQL("UPDATE poem SET ticai = 1 WHERE _id ="+ID);
140         //db.insert("book", null, contentValues);
141         db.close();
142         Toast.makeText(getApplicationContext(),
143                 "Collect succeed",
144                 Toast.LENGTH_SHORT).show();
145         //ID++;
146     }
147     @Override
148     public boolean onCreateOptionsMenu(Menu menu) {
149         // Inflate the menu; this adds items to the action bar if it is present.
150         MenuInflater inflater = getMenuInflater();
151         inflater.inflate(R.menu.menu_main, menu);
152         return super.onCreateOptionsMenu(menu);
153     }
154 
155     @Override
156     public boolean onOptionsItemSelected(MenuItem item) {
157         // Handle action bar item clicks here. The action bar will
158         // automatically handle clicks on the Home/Up button, so long
159         // as you specify a parent activity in AndroidManifest.xml.
160         int id = item.getItemId();
161         switch(id){
162             case R.id.collect:
163                 Intent intent = new Intent(this,Collect.class);
164                 startActivity(intent);
165                 break;
166             case R.id.like:
167                 AddaPoemToCollect();
168                 break;
169         }
170 
171         return super.onOptionsItemSelected(item);
172     }
173 }
View Code

相关文章: