1.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/mainbg"
tools:context=".MainActivity" >
<AutoCompleteTextView
android:id="@+id/etdanci"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="31dp"
android:background="@android:drawable/edit_text"
android:ems="10"
android:completionThreshold="3"
android:hint="@string/shuru"
android:singleLine="true"
android:textColor="#552006"
android:textColorHint="#782f10" >
<requestFocus />
</AutoCompleteTextView>
<TextView
android:id="@+id/tvxianshi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="@+id/etdanci"
android:layout_below="@+id/etdanci"
android:layout_marginTop="22dp"
android:textSize="25sp"
android:background="#55FFFFFF"
android:radius="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/btnchaxun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/tvxianshi"
android:layout_alignRight="@+id/tvxianshi"
android:background="@drawable/ibsearchword"
android:onClick="chaxundanci"
android:text="@string/chaxun" />
</RelativeLayout>
<pre class="html" name="code">2.MyCursorAdpter.java的代码,AutoCompleteTextView适配器的
package shipeiqi;
import android.R;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
public class MyCursorAdpter extends CursorAdapter{
private LayoutInflater layoutInflater;
@Override
public CharSequence convertToString(Cursor cursor) {
return cursor == null ? "" : cursor.getString(cursor
.getColumnIndex("_id"));
}
private void setView(View view, Cursor cursor) {
TextView tvWordItem = (TextView) view;
tvWordItem.setText(cursor.getString(cursor.getColumnIndex("_id")));
}
public MyCursorAdpter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
setView(view, cursor);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = layoutInflater.inflate(R.layout.simple_dropdown_item_1line, null);
setView(view, cursor);
return view;
}
}
3.DBfuzhu.java,辅助类的
package shujuku.fuzhu;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.example.cidian.R;
import com.example.cidian.R.raw;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;
public class DBfuzhu {
private final int Huanchongqu = 400000;
public static final String Shujukuname = "dictionary.db";
public static final String Baoming = "com.example.cidian";
public static final String shujukulujing = "/data" + Environment.getDataDirectory().getAbsolutePath() + Baoming + "/databases";
private Context context;
public DBfuzhu(Context context) {
this.context = context;
}
public SQLiteDatabase openDatabase() {
try {
File myDataPath = new File(shujukulujing);
if (!myDataPath.exists()) {
myDataPath.mkdirs();
}
String dbfile = myDataPath + "/" + Shujukuname;
if (!(new File(dbfile).exists())) {
InputStream is = context.getResources().openRawResource(R.raw.dictionary);
FileOutputStream fos = new FileOutputStream(dbfile);
byte[] buffer = new byte[Huanchongqu];
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;
}
}
4.wordDao.java类的
package shujuku.dao;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import shujuku.fuzhu.DBfuzhu;
public class wordDao {
private DBfuzhu dbfuzhu;
private SQLiteDatabase sqLiteDatabase;
public wordDao(Context context){
dbfuzhu = new DBfuzhu(context);
}
public String getchinese(String english){
sqLiteDatabase = dbfuzhu.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;
}
}
5.MainActivity.java类
package com.example.cidian;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
import shipeiqi.MyCursorAdpter;
import shujuku.dao.*;
import shujuku.fuzhu.DBfuzhu;
public class MainActivity extends Activity {
private DBfuzhu dbFuzhu;
private SQLiteDatabase database;
private wordDao wordDao;
private AutoCompleteTextView etdanci;
private TextView tvxianshi;
private Button chaxun;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chushihua();
dbFuzhu = new DBfuzhu(getBaseContext());// 打开数据库
database = dbFuzhu.openDatabase();
}
private void chushihua() {
etdanci = (AutoCompleteTextView) findViewById(R.id.etdanci);
tvxianshi = (TextView) findViewById(R.id.tvxianshi);
chaxun = (Button) findViewById(R.id.btnchaxun);
}
public void searchWord(View view) {
String english = etdanci.getText().toString();
wordDao = new wordDao(this);
if (TextUtils.isEmpty(english)) {
tvxianshi.setText("请输入您想查询的单词");
} else {
String chinese = wordDao.getchinese(english);
tvxianshi.setText(chinese);
}
}
}