单词本实现效果

继承自SQLiteOpenHelper的MyDataBaseHelper类

 1 package com.dj.wordsbookdemo;
 2 
 3 import android.content.Context;
 4 import android.database.sqlite.SQLiteDatabase;
 5 import android.database.sqlite.SQLiteDatabase.CursorFactory;
 6 import android.database.sqlite.SQLiteOpenHelper;
 7 
 8 public class MyDataBaseHelper extends SQLiteOpenHelper {
 9     
10 
11     public MyDataBaseHelper(Context context, String name,
12             CursorFactory factory, int version) {
13         super(context, name, factory, version);
14         
15     }
16     @Override
17     public void onCreate(SQLiteDatabase db) {
18          db.execSQL("CREATE TABLE IF NOT EXISTS words"+"(_id INTEGER PRIMARY KEY AUTOINCREMENT,word VARCHAR(20),mean VARCHAR(300))");
19     }
20 
21     @Override
22     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
23         
24     }
25 
26 }

主文件

 1 import android.app.Activity;
 2 import android.content.ContentValues;
 3 import android.database.Cursor;
 4 import android.database.sqlite.SQLiteDatabase;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.EditText;
 8 import android.widget.ListView;
 9 import android.widget.SimpleAdapter;
10 import android.widget.SimpleCursorAdapter;
11 
12 public class MainActivity extends Activity {
13     private SQLiteDatabase db;
14     private EditText et1, et2;
15     private ListView listview;
16     private MyDataBaseHelper helper;
17     String word;
18     String mean;
19 
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24         et1 = (EditText) findViewById(R.id.et1);
25         et2 = (EditText) findViewById(R.id.et2);
26         listview = (ListView) findViewById(R.id.listView1);
27         
28         helper=new MyDataBaseHelper(this, "sql.db", null, 1);
29         db = helper.getWritableDatabase();
30         
31         inflater();
32     }
33 
34     public void addWords(View v) {
35         word = et1.getText().toString();
36         mean = et2.getText().toString();
37         //添加单词的时候如果存在相同的,单词的意思被替换为本次的。
38         if (db.query("words", null, "word=?", new String[]{word}, null, null, null).getCount()>0) {
39             //execSQL()方法
40             db.execSQL("update words set mean='"+mean+"' where word='"+word+"'");
41             //db.update()方法
42             /*ContentValues values = new ContentValues();
43             values.put("mean", mean);
44             db.update("words", values, "word=?", new String[] { word });*/
45         }else{
46 
47         ContentValues values = new ContentValues();
48         values.put("word", word);
49         values.put("mean", mean);
50         db.insert("words", null, values);
51         }
52         //添加数据后就刷新listview的内容
53         inflater();
54     }
55     
56     //删除按钮监听器
57     public void deleteWords(View v) {
58         word = et1.getText().toString();
59         if (word.equals("")) {
60             et1.setError("请输入要删除的单词");
61         }
62         String[] whereArgs={word};
63         db.delete("words", "word=?", whereArgs);
64         //删除数据后刷新listview的内容
65         inflater();
66     }
67     //查询按钮监听器
68     public void findWords(View v){
69         word = et1.getText().toString();
70         //根据单词查找数据
71         String sql="select * from words where word=?";
72         Cursor cursor = db.rawQuery(sql, new String[]{word});
73         SimpleCursorAdapter adapter=new SimpleCursorAdapter(MainActivity.this, R.layout.data, cursor, new String[]{"word","mean"}, new int[]{R.id.textView1, R.id.textView2},SimpleCursorAdapter.IGNORE_ITEM_VIEW_TYPE);
74         listview.setAdapter(adapter);
75     }
76     
77     //定义一个inflater()方法用于及时的刷新数据
78     public void inflater() {
79         String sql = "select * from words";
80         Cursor cursor = db.rawQuery(sql, null);
81         SimpleCursorAdapter adapter = new SimpleCursorAdapter(
82                 MainActivity.this, R.layout.data, cursor, new String[] {"word", "mean" }, new int[] { R.id.textView1,R.id.textView2 },
83                 SimpleCursorAdapter.IGNORE_ITEM_VIEW_TYPE);
84                 listview.setAdapter(adapter);
85     }
86 
87 }

 

 

main.xml文件

 1  2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context=".MainActivity" >
11 
12     <EditText
13         android:id="@+id/et1"
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:hint="单词" />
17 
18     <EditText
19         android:id="@+id/et2"
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:hint="释意" />
23 
24     <LinearLayout
25         android:layout_width="match_parent"
26         android:layout_height="wrap_content"
27         android:gravity="center_horizontal" >
28 
29         <Button
30             android:layout_width="wrap_content"
31             android:layout_height="wrap_content"
32             android:onClick="addWords"
33             android:text="添加生词" />
34 
35         <Button
36             android:layout_width="wrap_content"
37             android:layout_height="wrap_content"
38             android:onClick="deleteWords"
39             android:text="删除单词" />
40 
41         <Button
42             android:id="@+id/button1"
43             android:layout_width="wrap_content"
44             android:layout_height="wrap_content"
45             android:onClick="findWords"
46             android:text="查找单词" />
47     
48 
49     <ListView
50         android:id="@+id/listView1"
51         android:layout_width="match_parent"
52         android:layout_height="match_parent" >
53     
54 
55 
View Code

data.xml文件,simpcursoadapter用到

 1 
 2  3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <LinearLayout
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content" >
10 
11         <TextView
12             android:layout_width="wrap_content"
13             android:layout_height="wrap_content"
14             android:text="单词:" />
15 
16         <TextView
17             android:id="@+id/textView1"
18             android:layout_width="wrap_content"
19             android:layout_height="wrap_content" />
20     
21 
22     <LinearLayout
23         android:layout_width="match_parent"
24         android:layout_height="wrap_content" >
25 
26         <TextView
27             android:layout_width="wrap_content"
28             android:layout_height="wrap_content"
29             android:text="意思:" />
30 
31         <TextView
32             android:id="@+id/textView2"
33             android:layout_width="wrap_content"
34             android:layout_height="wrap_content" />
35     
36 
37 
View Code

 

效果图

单词本实现效果_第1张图片

 

 

转载于:https://www.cnblogs.com/dj168/p/sqlite.html

你可能感兴趣的:(单词本实现效果)