一:软件介绍。
乐学成语是一款关于成语学习的小软件,主要具备以下的几点功能:
1.用户可以通过分类(动物类,人物类等等)来进行学习。
2.学习之余,也有适当的成语游戏。
3.可以根据用户的需要对成语进行收藏。
4.具有对成语的查询功能。
二:步骤。
1.创建数据库和表。
2.显示主界面。
3.显示学习列表
。
4.显示动物类的成语。
5.显示每条成语的详细内容
三.主要代码。
MainActivity
package cn.edu.bztc.happyidion.activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.Window;
import android.widget.TabHost;
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(); //获取TabHost实例
/*一个Tab对应一个name名称,一个标题,一个图标,yigeActivity页面*/
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);
}
/*定义每个Tab的显示内容*/
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) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
AnimalAdapter
package cn.edu.bztc.happyidiom.adapter;
import java.util.List;
import cn.edu.bztc.happyidiom.entity.Animal;
import cn.edu.bztc.happyidion.activity.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class AnimalAdapter extends ArrayAdapter<Animal>{
private int resourceId;
private Context context;
public AnimalAdapter(Context context, int resource,List<Animal> objects) {
super(context,resource, objects);
this.context=context;
resourceId=resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Animal animal =getItem(position);//获取当前项的Animal实例
View view;
ViewHolder viewHolder;
if(convertView==null){//判断是否第一次运行,如果是则进入,并将上下文环境保存进convertView
view=LayoutInflater.from(getContext()).inflate(resourceId,null);
viewHolder=new ViewHolder();
viewHolder.tvName=(TextView)view.findViewById(R.id.tvName);
viewHolder.btnSave=(ImageButton)view.findViewById(R.id.btnSave);
viewHolder.btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(context,"你要收藏"+animal.getName()+"吗",Toast.LENGTH_SHORT).show();
}
});
view.setTag(viewHolder);
}else{//如果不是第一次运行,convertView不为空,直接取出赋值给view
view=convertView;
viewHolder=(ViewHolder) view.getTag();
}
viewHolder.tvName.setText(animal.getName());//显示成语
return view;
}
class ViewHolder{
TextView tvName;
ImageButton btnSave;
}
}
CategoryAdapter
package cn.edu.bztc.happyidiom.adapter;
import java.util.List;
import cn.edu.bztc.happyidiom.entity.Category;
import cn.edu.bztc.happyidion.activity.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CategoryAdapter extends ArrayAdapter<Category>{
private int resourceId;
public CategoryAdapter(Context context, int resource,
List<Category> categoryList) {
super(context,resource, categoryList);
resourceId=resource;
}
public View getView(int position, android.view.View convertView, android.view.ViewGroup parent) {
Category category=getItem(position);//获取当前项的Category实例
View view;
ViewHolder viewHolder;
if(convertView==null){
view=LayoutInflater.from(getContext()).inflate(resourceId,null);
viewHolder=new ViewHolder();
viewHolder.categoryImage=(ImageView)view.findViewById(R.id.category_image);
viewHolder.categoryName=(TextView)view.findViewById(R.id.category_name);
view.setTag(viewHolder);
}else{
view=convertView;
viewHolder=(ViewHolder) view.getTag();
}
viewHolder.categoryImage.setImageResource(category.getImageId());
viewHolder.categoryName.setText(category.getName());
return view;
}
class ViewHolder{
ImageView categoryImage;
TextView categoryName;
}
}
AnimalDao
package cn.edu.bztc.happyidiom.dao;
import java.util.ArrayList;
import java.util.List;
import cn.edu.bztc.happyidiom.db.DBOpenHelper;
import cn.edu.bztc.happyidiom.entity.Animal;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class AnimalDao {
private static AnimalDao animaiDao;
private SQLiteDatabase db;
/*将构造方法私有化*/
private AnimalDao(Context context){
DBOpenHelper dbHelper=new DBOpenHelper(context);
db=dbHelper.openDatabase();
}
/*获取AnimalDao的实例*/
public synchronized static AnimalDao getInstance(Context context){
if(animaiDao==null){
animaiDao=new AnimalDao(context);
}
return animaiDao;
}
/*从数据库读取所有的动物类成语*/
public List<Animal> getAllAnimals(){
List<Animal> list=new ArrayList<Animal>();
Cursor cursor=db.query("animal",null,null,null,null,null,null);
if(cursor.moveToNext()){
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.setAntonym(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("examples")));
animal.setExplain(cursor.getString(cursor.getColumnIndex("explain")));
list.add(animal);
}while(cursor.moveToNext());
}
return list;
}
}
DBOpenHelper
package cn.edu.bztc.happyidiom.db;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import cn.edu.bztc.happyidion.activity.R;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;
/*实现将数据库文件从raw目录拷贝到手机里存放数据库的位置*/
public class DBOpenHelper {
private final int BUFFER_SIZE=400000;//缓冲区大小
public static final String DB_NAME="idioms.db";//保存的数据库文件名
public static final String PACKAGE_NAME="cn.edu.bztc.happyidion.activity";//应用的包名
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("MainActivity","File not found");
e.printStackTrace();
}catch (IOException e) {
Log.e("MainActivity","IO exception");
e.printStackTrace();
}
return null;
}
}
Animal
package cn.edu.bztc.happyidiom.entity;
public class Animal {
private int id;
private String name;//成语名称
private String pronounce;//成语发音
private String explain;//成语解释
private String antonym;//反义词
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 getAntonym() {
return antonym;
}
public void setAntonym(String antonym) {
this.antonym = antonym;
}
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;
}
}
Category
package cn.edu.bztc.happyidiom.entity;
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;
}
}