SQLite 辞典实例
2019-06-13 本文已影响0人
被罚站的树
image.png
image.png
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:textSize="80sp"
android:layout_marginTop="20dp"
android:text=" 辞典"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:textSize="14sp"
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="160dp"
/>
<Button
android:text="翻译"
android:id="@+id/btn_fanyi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_marginTop="90dp"
android:layout_marginRight="30dp"
android:layout_alignParentRight="true"
/>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_below="@id/btn_fanyi"
android:layout_marginTop="40dp"
>
</ListView>
<Button
android:text="添加新词"
android:id="@+id/btn_tianjia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/listView"
android:layout_marginRight="30dp"
android:layout_alignParentRight="true"
android:layout_marginTop="20dp"
/>
<TextView
android:textSize="20dp"
android:layout_marginTop="20dp"
android:id="@+id/xianshi"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@id/listView"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".addActivity"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
>
<EditText
android:layout_marginTop="150dp"
android:id="@+id/ed_danci"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/ed_jiexi"
android:layout_marginTop="100dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:textSize="30sp"
android:text="确定"
android:layout_marginTop="200dp"
android:id="@+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_marginTop="20dp"
android:textSize="30sp"
android:text="取消"
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private DBOpenHelper dbOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化DBOpenHelper对象用来创建数据库
dbOpenHelper=new DBOpenHelper(MainActivity.this,"db_dict",null,1);
final ListView listView=findViewById(R.id.listView);
final EditText editText=findViewById(R.id.editText);
Button btn_fanyi=findViewById(R.id.btn_fanyi);
Button btn_tianjia=findViewById(R.id.btn_tianjia);
final TextView xianshi=findViewById(R.id.xianshi);
btn_fanyi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String key=editText.getText().toString();//获取要查询的单词
//执行查询操作
Cursor cursor=dbOpenHelper.getReadableDatabase().query("tb_dict",null,"word=?"
,new String[]{key},null,null,null);
//创建ArrayList对象用于保存查询结果
ArrayList<Map<String,String>> resultList=new ArrayList<Map<String, String>>();
while(cursor.moveToNext()){
Map<String,String>map=new HashMap<String, String>();
map.put("word",cursor.getString(1));
map.put("interpret",cursor.getString(2));
resultList.add(map);
}
if(resultList==null||resultList.size()==0){
//提示没有相关信息
Toast.makeText(MainActivity.this, "没有相关信息", Toast.LENGTH_SHORT).show();
}else{
// SimpleAdapter simpleAdapter=new SimpleAdapter(MainActivity.this,resultList,
// R.id.result_main,R.id.result_interpret);
String result=resultList.toString();
xianshi.setText(result);
}
}
});
//跳转到添加页面
btn_tianjia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,addActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if(dbOpenHelper!=null){
dbOpenHelper.close();//关闭数据库连接
}
}
}
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBOpenHelper extends SQLiteOpenHelper {
//创建数据表的SQL语句
final String CREATE_TABLE_SQL="create table tb_dict (_id integer primary key autoincrement,word,detail)";
public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建单词的数据表
db.execSQL(CREATE_TABLE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//数据库版本更新
Log.i("辞典","--版本更新"+oldVersion+"-->"+newVersion);
}
}
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class addActivity extends AppCompatActivity {
private DBOpenHelper dbOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
Button btn_ok=findViewById(R.id.btn_ok);
Button btn_cancel=findViewById(R.id.btn_cancel);
final EditText ed_danci=findViewById(R.id.ed_danci);
final EditText ed_jiexi=findViewById(R.id.ed_jiexi);
//实例化DBOpenHelper对象
dbOpenHelper=new DBOpenHelper(addActivity.this,"db_dict",null,1);
btn_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String danci=ed_danci.getText().toString();//获取单词
String jiexi=ed_jiexi.getText().toString();
if(danci.equals("")||jiexi.equals("")){
Toast.makeText(addActivity.this, "生词或解析为空,请重新输入", Toast.LENGTH_SHORT).show();
}else {
insertData(dbOpenHelper.getReadableDatabase(),danci,jiexi);
Toast.makeText(addActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
}
}
});
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//实现由副页面跳转回主页面
Intent intent=new Intent(addActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
//插入数据的方法
private void insertData(SQLiteDatabase sqLiteDatabase,String word,String interpret){
ContentValues values=new ContentValues();
values.put("word",word);
values.put("detail",interpret);
sqLiteDatabase.insert("tb_dict",null,values);
}
@Override
protected void onDestroy() {
if(dbOpenHelper!=null){
dbOpenHelper.close();
}
super.onDestroy();
}
}