热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

本地数据库词典

本文章采用主要引用了本地数据库进行词典构建,之前上传的网页版的词典也是在此基础上进行改造,因此把本地数据库版的版本上传分享一下,依然添加下

本文章采用主要引用了本地数据库进行词典构建,之前上传的网页版的词典也是在此基础上进行改造,因此把本地数据库版的版本上传分享一下,依然添加下拉菜单,此内容属于本地数据库进行连接进行自动提示。

1.MainActivity

package com.example.happydictionary;import java.util.ArrayList;
import java.util.List;import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;import com.example.happydictionary.dao.WordDao;
import com.example.happydictionary.db.DBHelper;public class MainActivity extends Activity {private WordDao wordDao;private EditText etWord;private TextView tvResult;private AutoCompleteTextView ac;// protected void onCreate1(Bundle savedInstanceState) {// super.onCreate(savedInstanceState);// setContentView(R.layout.activity_main);//// initViews();// }@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);SetView();}// private void initViews() {// // TODO Auto-generated method stub//// }private Button btnSearch;// private EditText editText;private WebView reswebView;private WebView wvSearchResult;private void SetView() {btnSearch = (Button) findViewById(R.id.btnSearch);// etWord = (EditText) findViewById(R.id.etWord);tvResult = (TextView) findViewById(R.id.tvSearchResult);ac = (AutoCompleteTextView)findViewById(R.id.etWord);/*** 原来的editText换成了AutoCompleteTextView*/DBHelper helper = new DBHelper(MainActivity.this);SQLiteDatabase db = helper.openDatabase();// Cursor cur = db.query("dictionary", new String[] { "t_words" },// null, null, null, null, null);String sql = "select english from t_words";Cursor cur = db.rawQuery(sql, null);int i = 0;int j = 0;List list = new ArrayList();while (cur.moveToNext()) {String chinese = cur.getString(cur.getColumnIndex("english"));list.add(chinese);}//// String[] countries = new String[] { "Afghanistan", "Albania",// "Algeria", "American Samoa", "Andorra" };// 这里只举例说明,数据较少AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.etWord);// 找到相应的控件ArrayAdapter adapter = new ArrayAdapter(this,R.layout.list_item, list);// 配置AdaptorautoCompleteTextView.setAdapter(adapter);btnSearch.setOnClickListener(new OnClickListener() {public void onClick(View v) {
// String strUri = etWord.getText().toString();
// strUri = strUri.trim();
// if (strUri.length() == 0) {
// Toast.makeText(getApplicationContext(), "请输入查询字符", 1)
// .show();
// } else {
// String strURL = "http://dict.youdao.com/m/search?keyfrom=dict.mindex&q="
// + strUri;
// // 这个地方抛出空指针异常,我没学过这个方法,没办法,不知道这个有道接口格式
// reswebView.loadUrl(strURL);
// }WordDao wd = new WordDao(MainActivity.this);String chinese = wd.getChinese(ac.getText().toString());tvResult.setText(ac.getText().toString()+"\n"+chinese);}});}}



2.WordDao.java

package com.example.happydictionary.dao;import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;import com.example.happydictionary.db.DBHelper;public class WordDao {private DBHelper dbHelper;private SQLiteDatabase sqLiteDatabase;public WordDao(Context context){dbHelper=new DBHelper(context);}public String getChinese(String english){sqLiteDatabase=dbHelper.openDatabase();String sql="select chinese from t_words where english=?";Cursor cursor=sqLiteDatabase.rawQuery(sql, new String[]{english});String chinese="查无该词";if(cursor.moveToFirst()){chinese=cursor.getString(cursor.getColumnIndex("chinese"));}return chinese;}
}



3.DBHelper.java

package com.example.happydictionary.db;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;import com.example.happydictionary.R;
import com.example.happydictionary.R.raw;import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;/*** 实现将数据库文件从raw目录拷贝到手机里存放数据库的位置* * @author cabbage*/
public class DBHelper {private final int BUFFER_SIZE = 400000;public static final String DB_NAME = "idiom.db"; // 保存的数据库文件名public static final String PACKAGE_NAME = "com.example.happydictionary";// 应用的包名public static final String DB_PATH = "/data"+ Environment.getDataDirectory().getAbsolutePath() + "/"+ PACKAGE_NAME + "/databases";/*// SDCard 定义数据库的存放路径private final String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/dictionary";*/private Context context;public DBHelper(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.dictionary); // 欲导入的数据库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);}Log.v("###", "bucunzai");fos.close();is.close();}SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,null);Log.v("###", "cunzai");return db;} catch (FileNotFoundException e) {Log.e("###", "File not found");e.printStackTrace();} catch (IOException e) {Log.e("###", "IO exception");e.printStackTrace();}return null;}
}



4.Word.java

package com.example.happydictionary.entity;public class Word {private String english;private String chinese;public String getEnglish() {return english;}public void setEnglish(String english) {this.english = english;}public String getChinese() {return chinese;}public void setChinese(String chinese) {this.chinese = chinese;}}



5.DBHelperTest.java

package com.example.happydictionary.test;import com.example.happydictionary.db.DBHelper;import android.test.AndroidTestCase;public class DBHelperTest extends AndroidTestCase {public void testCreateDB(){DBHelper dbHelper=new DBHelper(getContext());dbHelper.openDatabase();}
}



6.activity_mian.xml


7.list_item.xml






8.menu>mian.xml




9.strings.xml


电子词典SettingsHello world!请输入您要查询的单词查询






推荐阅读
author-avatar
张嫱的小屋_133
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有