android SQLite(单词的添加与查询应用)
本人小白,刚接触android,为方便记忆,将平时练习的代码写下来,跟大家分享,也希望大神批评指正。
这个实例主要用到的SQLite数据库的操作,可以向数据库添加单词,查询,修改以及删除单词,描述如有不当之处,还请帮忙纠正,下面上源码。
------------------------------------------我是邪恶的分割线----------------------------------------------
下面是java文件
1.创建数据库:
1 import android.content.Context; 2 import android.database.sqlite.SQLiteDatabase; 3 import android.database.sqlite.SQLiteOpenHelper; 4 5 public class DatabaseHelper extends SQLiteOpenHelper { 6 7 //定义创建数据表的语句 8 private static final String CREATE_TABLES="create table word_dic(_id INTEGERT PRIMARY KEY AUTOINCREMENT,word,detail)"; 9 10 //定义构造函数 11 public DatabaseHelper(Context context,String name,int version){ 12 super(context,name,null,version); 13 } 14 @Override 15 public void onCreate(SQLiteDatabase db) { 16 // TODO Auto-generated method stub 17 db.execSQL(CREATE_TABLES); 18 } 19 20 @Override 21 public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { 22 // TODO Auto-generated method stub 23 24 } 25 26 }
2.创建界面UI
2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 6 import android.app.Activity; 7 import android.content.ContentValues; 8 import android.content.Intent; 9 import android.database.Cursor; 10 import android.database.sqlite.SQLiteDatabase; 11 import android.os.Bundle; 12 import android.view.Menu; 13 import android.view.View; 14 import android.view.View.OnClickListener; 15 import android.widget.Button; 16 import android.widget.EditText; 17 import android.widget.Toast; 18 19 public class MainActivity extends Activity { 20 private EditText word; 21 private EditText detail; 22 private Button insert; 23 private Button query; 24 private Button update; 25 private Button delete; 26 DatabaseHelper dbHelper=null; 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.main); 31 32 //获取控件对象 33 word=(EditText)findViewById(R.id.word); 34 detail=(EditText)findViewById(R.id.detail); 35 insert=(Button)findViewById(R.id.insert); 36 query=(Button)findViewById(R.id.query); 37 update=(Button)findViewById(R.id.update); 38 delete=(Button)findViewById(R.id.delete); 39 40 //生成DatabaseHelper对象 41 dbHelper=new DatabaseHelper(MainActivity.this,"english_db",1); 42 43 //两个EditText用来获取用户的输入 44 //String wordStr=word.getText().toString(); 45 //String detailStr=detail.getText().toString(); 46 47 //************************** 48 //为按钮绑定相应的监听器 49 //************************** 50 51 //添加单词 52 insert.setOnClickListener(new OnClickListener() { 53 54 @Override 55 public void onClick(View v) { 56 // TODO Auto-generated method stub 57 SQLiteDatabase db=dbHelper.getWritableDatabase(); 58 String wordStr=word.getText().toString(); 59 String detailStr=detail.getText().toString(); 60 ContentValues values=new ContentValues(); 61 values.put("word", wordStr); 62 values.put("detail", detailStr); 63 db.insert("word_dic", null, values); 64 Toast.makeText(MainActivity.this, "添加单词成功", Toast.LENGTH_LONG).show(); 65 } 66 }); 67 68 //查询单词 69 query.setOnClickListener(new OnClickListener() { 70 71 @Override 72 public void onClick(View v) { 73 // TODO Auto-generated method stub 74 SQLiteDatabase db=dbHelper.getReadableDatabase(); 75 String wordStr=word.getText().toString(); 76 77 Cursor cursor=db.query("word_dic", new String[]{"_id","word","detail"}, "word like ?", 78 new String[]{wordStr},null, null, null); 79 ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>(); 80 while(cursor.moveToNext()){ 81 HashMap<String, String> map=new HashMap<String, String>(); 82 map.put("word", cursor.getString(cursor.getColumnIndex("word"))); 83 map.put("detail", cursor.getString(cursor.getColumnIndex("detail"))); 84 list.add(map); 85 } 86 Intent intent=new Intent(MainActivity.this,OtherActivity.class); 87 Bundle bundle=new Bundle(); 88 bundle.putSerializable("data", list); 89 intent.putExtras(bundle); 90 startActivity(intent); 91 } 92 }); 93 94 //修改单词 95 update.setOnClickListener(new OnClickListener() { 96 97 @Override 98 public void onClick(View v) { 99 // TODO Auto-generated method stub 100 SQLiteDatabase db=dbHelper.getWritableDatabase(); 101 String wordStr=word.getText().toString(); 102 String detailStr=detail.getText().toString(); 103 ContentValues values=new ContentValues(); 104 values.put("word", wordStr); 105 values.put("detail", detailStr); 106 db.update("word_dic", values, "word like ?", new String[]{wordStr}); 107 Toast.makeText(MainActivity.this, "单词已更新", Toast.LENGTH_LONG).show(); 108 } 109 }); 110 111 //删除单词 112 delete.setOnClickListener(new OnClickListener() { 113 114 @Override 115 public void onClick(View v) { 116 // TODO Auto-generated method stub 117 SQLiteDatabase db=dbHelper.getWritableDatabase(); 118 String wordStr=word.getText().toString(); 119 120 db.delete("word_dic", "word like ?", new String[]{wordStr}); 121 Toast.makeText(MainActivity.this, "单词已删除", Toast.LENGTH_LONG).show(); 122 } 123 }); 124 } 125 126 @Override 127 public boolean onCreateOptionsMenu(Menu menu) { 128 // Inflate the menu; this adds items to the action bar if it is present. 129 getMenuInflater().inflate(R.menu.main, menu); 130 return true; 131 } 132 133 }
3.创建查询结果的ACTIVITY:
1 import java.util.ArrayList; 2 import java.util.HashMap; 3 4 import android.app.Activity; 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.widget.ListView; 8 import android.widget.SimpleAdapter; 9 10 public class OtherActivity extends Activity { 11 private ListView listView; 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 // TODO Auto-generated method stub 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.result); 17 18 listView=(ListView)findViewById(R.id.listView); 19 Intent intent=getIntent(); 20 Bundle bundle=intent.getExtras(); 21 22 @SuppressWarnings("unchecked") 23 ArrayList<HashMap<String, String>> list=(ArrayList<HashMap<String,String>>)bundle.getSerializable("data"); 24 SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.option, new String[]{"word","detail"}, 25 new int[]{R.id.wordShow,R.id.detailShow}); 26 listView.setAdapter(adapter); 27 } 28 29 }
*********************************以下是用到的资源文件*********************************************
1.main.xml:
1 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="fill_parent" 3 android:layout_height="fill_parent" > 4 <TableRow android:paddingLeft="10dp"> 5 <TextView 6 android:layout_width="50dp" 7 android:layout_height="wrap_content" 8 android:text="单词:" 9 android:textSize="16sp" 10 /> 11 <EditText 12 android:id="@+id/word" 13 android:layout_width="250dp" 14 android:layout_height="wrap_content" 15 android:selectAllOnFocus="true" 16 android:hint="请输入单词" 17 /> 18 </TableRow> 19 <TableRow android:paddingLeft="10dp"> 20 <TextView 21 android:layout_width="50dp" 22 android:layout_height="wrap_content" 23 android:text="释意:" 24 android:textSize="16sp" 25 /> 26 <EditText 27 android:id="@+id/detail" 28 android:layout_width="250dp" 29 android:layout_height="wrap_content" 30 android:selectAllOnFocus="true" 31 android:hint="请输入单词的解释" 32 /> 33 </TableRow> 34 <Button 35 android:id="@+id/insert" 36 android:layout_width="fill_parent" 37 android:layout_height="wrap_content" 38 android:text="添加单词" 39 /> 40 <Button 41 android:id="@+id/query" 42 android:layout_width="fill_parent" 43 android:layout_height="wrap_content" 44 android:text="查询单词" 45 /> 46 <Button 47 android:id="@+id/update" 48 android:layout_width="fill_parent" 49 android:layout_height="wrap_content" 50 android:text="修改单词" 51 /> 52 <Button 53 android:id="@+id/delete" 54 android:layout_width="fill_parent" 55 android:layout_height="wrap_content" 56 android:text="删除单词" 57 /> 58 </TableLayout>
2.result.xml:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="fill_parent" 3 android:layout_height="fill_parent" 4 android:orientation="vertical" > 5 6 <ListView 7 android:id="@+id/listView" 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" > 10 </ListView> 11 12 </LinearLayout>
3.option.xml:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="fill_parent" 3 android:layout_height="fill_parent" > 4 5 <TextView 6 android:id="@+id/wordShow" 7 android:layout_width="100dp" 8 android:layout_height="wrap_content" 9 android:layout_alignParentLeft="true" 10 android:layout_alignParentTop="true" 11 /> 12 13 <TextView 14 android:id="@+id/detailShow" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:layout_alignParentRight="true" 18 android:layout_alignParentTop="true" 19 android:layout_toRightOf="@+id/wordShow" 20 /> 21 22 </RelativeLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。