一、功能需求:
1.成语分类学习:用户可以根据自己喜欢的分类来进行选择学习,其中包括(动物类、人物类、季节类、自然类、数字类、寓言类、其他类)7个类。
2.乐猜成语:学习之余玩玩猜成语游戏,寓教于乐。
3.成语收藏:当用户遇到自己想要保存的成语,点击保存按钮,即可以保存到收藏页面以方便用户再次查看,对已经掌握的成语,用户可以从收藏夹中删除。
4.成语查询:输入成语的首字母即可查询该成语。
二、创建包和数据库:
1.在com.edu.happyidiom下创建几个新包:
其中adapter包用于存放所有活动相关的代码,db包用于存放所有数据库相关的代码,entity包用于存放所有实体相关的代码,dao包用于存放所有与数据操作相关的代码。ADT自动生成的MainActivity个activity_main.xml就不需要了,这里直接将它们删掉。
2.创建数据库和表:
三、在各个包中新建与所实现功能对应的类,并编写代码:
1.DBOpenHelper类,实现将数据库文件从raw目录拷贝到手机里存放数据库的位置:
public class DBOpenHelper {
private final int BUFFER_SIZE=400000;//缓冲区大小
public static final String DB_NAME="idioms.db"; //保存的数据库文件名
public static final String PACKAGE_NAME="com.example.happyidiom";//应用的包名
public static final String DB_PATH="/data"
+Environment.getDataDirectory().getAbsolutePath()+"/"
+PACKAGE_NAME+"/databases";//在手机里存放数据库的位置
private Context context;
public DBOpenHelper(Context context){
this.context=context;
}
public SQLiteDatabase openDatabase(){
try{
File myDataPath=new File(DB_PATH);
if(!myDataPath.exists()){
myDataPath.mkdirs();//如果没有这个数据库则创建
}
String dbfile=myDataPath+"/"+DB_NAME;
if(!(new File(dbfile).exists())){//判断数据库文件是否存在,若不存在直接导入,
//否则直接打开数据库
InputStream is=context.getResources().openRawResource(R.raw.idioms);
FileOutputStream fos=new FileOutputStream(dbfile);
byte[] buffer=new byte[BUFFER_SIZE];
int count=0;
while((count=is.read(buffer))>0){
fos.write(buffer, 0,count);
}
fos.close();
is.close();
}
SQLiteDatabase db=SQLiteDatabase.openOrCreateDatabase(dbfile, null);
return db;
}catch(FileNotFoundException e){
Log.e("Database","File not found");
e.printStackTrace();
}
catch(IOException e){
Log.e("Database","IO exception");
e.printStackTrace();
}
return null;
}
}
2.为了检验idioms.db中数据有没有复制成功,需要修改AndroidManifest.xml搭建单元测试环境:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.happyidiom"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="android.test.runner" />
<activity
android:name="com.example.happyidiom.MainActivity"
android:label="@string/title_activity_main"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.happyidiom.StudyActivity"
android:label="@string/title_activity_study" >
</activity>
<activity
android:name="com.example.happyidiom.StudyAnimalActivity"
android:label="@string/title_activity_study_animal" >
</activity>
<activity
android:name="com.example.happyidiom.StudyOtherActivity"
android:label="@string/title_activity_study_other" >
</activity>
</application>
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.happyidiom" >
</instrumentation>
</manifest>
3.检验数据库有没有复制成功:
4.新建Animal实体类:
public class Animal {
private int id;
private String name;
private String pronounce;
private String explain;
private String autonym;
private String homoionym;
private String derivation;
private String examples;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPronounce() {
return pronounce;
}
public void setPronounce(String pronounce) {
this.pronounce = pronounce;
}
public String getExplain() {
return explain;
}
public void setExplain(String explain) {
this.explain = explain;
}
public String getAutonym() {
return autonym;
}
public void setAutonym(String autonym) {
this.autonym = autonym;
}
public String getHomoionym() {
return homoionym;
}
public void setHomoionym(String homoionym) {
this.homoionym = homoionym;
}
public String getDerivation() {
return derivation;
}
public void setDerivation(String derivation) {
this.derivation = derivation;
}
public String getExamples() {
return examples;
}
public void setExamples(String examples) {
this.examples = examples;
}
}
5.创建AnimalDao类:
public class AnimalDao {
private static AnimalDao animalDao;
private SQLiteDatabase db;
/**
* 将构造方法私有化
*/
private AnimalDao(Context context){
DBOpenHelper dbHelper =new DBOpenHelper(context);
db=dbHelper.openDatabase();
}
/**
* 获取AnimalDao的实例
*/
public synchronized static AnimalDao getInstance(Context context){
if(animalDao==null){
animalDao=new AnimalDao(context);
}
return animalDao;
}
/**
* 从数据库获取所有的动物类成语
*/
public List<Animal> getAllAnimals(){
List<Animal> list=new ArrayList<Animal>();
Cursor cursor =db.query("animal",null, null,null,null,null,null);
if(cursor.moveToFirst()){
do{
Animal animal=new Animal();
animal.setId(cursor.getInt(cursor.getColumnIndex("_id")));
animal.setName(cursor.getString(cursor.getColumnIndex("name")));
animal.setPronounce(cursor.getString(cursor.getColumnIndex("pronounce")));
animal.setAutonym(cursor.getString(cursor.getColumnIndex("antonym")));
animal.setHomoionym(cursor.getString(cursor.getColumnIndex("homoionym")));
animal.setDerivation(cursor.getString(cursor.getColumnIndex("derivation")));
animal.setExamples(cursor.getString(cursor.getColumnIndex("explain")));
list.add(animal);
}while(cursor.moveToNext());
}
return list;
}
}
6.运行单元测试:
四、显示主界面:
1.在res的drawable-hdpi目录拷入需要的图片素材,在res/layout目录中新建activity_main.xml布局,代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<LinearLayout
android:id="@+id/tab1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</LinearLayout>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TabWidget>
</LinearLayout>
</TabHost>
</RelativeLayout>
2.修改res/strings.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">HappyIdiom</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_search">搜搜</string>
<string name="title_study">学习</string>
<string name="title_game">游戏</string>
<string name="title_save">收藏</string>
<string name="title_help">帮助</string>
<string name="animal">动物类</string>
<string name="number">数字类</string>
<string-array name="category">
<item>动物类</item>
<item>数字类</item>
<item>人物类</item>
<item>季节类</item>
<item>自然类</item>
<item>寓言类</item>
<item>其他类</item>
</string-array>
<string name="title_activity_study">StudyActivity</string>
<string name="title_activity_study_animal">StudyAnimalActivity</string>
<string name="title_activity_study_other">StudyOtherActivity</string>
</resources>
五、
1.在com.example.happyidiom中创建MainActivity继承Avtivity类:
public class MainActivity extends TabActivity {
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
tabHost=getTabHost();
addTab("study", R.string.title_study, R.drawable.study,StudyActivity.class);
addTab("search", R.string.title_search, R.drawable.search, StudyActivity.class);
addTab("game", R.string.title_game, R.drawable.game, StudyActivity.class);
addTab("save", R.string.title_save, R.drawable.save, StudyActivity.class);
addTab("help", R.string.title_help, R.drawable.help, StudyActivity.class);
}
private void addTab(String tag,int title_introduction,int title_icon,Class ActivityClass){
tabHost.addTab(tabHost.newTabSpec(tag)
.setIndicator(getString(title_introduction),
getResources().getDrawable(title_icon))
.setContent(new Intent(this,ActivityClass)));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
2.定义一个实体类,作为ListView适配器的适配类型。在entity包下创建Category:
public class Category {
private String name;
private int imageId;
public Category(String name, int imageId) {
super();
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
}
3.
在com.example.happyidiom中创建StudyActivity继承Avtivity类:
public class StudyActivity extends Activity {
private List<Category> categoryList;
private String[] category_names;
private int[] category_images;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_study);
initCategory();
CategoryAdapter adapter=new CategoryAdapter(this, R.layout.category_item, categoryList);
ListView listView=(ListView) findViewById(R.id.lvCategories);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
// TODO Auto-generated method stub
Intent intent;
switch(position){
case 0:
intent=new Intent(StudyActivity.this,StudyAnimalActivity.class);
startActivity(intent);
break;
case 6:
intent=new Intent(StudyActivity.this,StudyOtherActivity.class);
startActivity(intent);
break;
default:
break;
}
}
});
}
private void initCategory() {
// TODO Auto-generated method stub
categoryList=new ArrayList<Category>();
Resources resources=getResources();
category_names=resources.getStringArray(R.array.category);
category_images=new int[]{R.drawable.category_animal,
R.drawable.category_nature,R.drawable.category_human,
R.drawable.category_season,R.drawable.category_number,
R.drawable.category_fable,R.drawable.category_other
};
for(int i=0;i<category_names.length;i++){
categoryList.add(new Category(category_names[i], category_images[i]));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.study, menu);
return true;
}
}
特此声明:其他的几个类与Animal类相似。