功能实现单词本的添加与查询单词
查询结果图如下
代码如下:
查询结果使用的activity是dialog风格的activity,在manifest文件中如此配置
- <activity android:name&#61;".ResultActivity"
- android:label&#61;"查询结果"
- android:theme&#61;"&#64;android:style/Theme.Dialog">activity>
要实现数据库的增删改查&#xff0c;需要三步&#xff1a;
1.继承SQLiteOpenHelper复写onCreate方法&#xff0c;一般在里面实现创建表的工作。
2.通过SQLiteOpenHelper的getReadableDatabase或者getWriteableDatabase方法创建数据库
3.调用数据库增删改查方法进行数据操作
java代码如下
- package com.example.mysqliitedatabase;
- import android.provider.BaseColumns;
- import android.provider.SyncStateContract.Columns;
- //定义数据库表的名字及表的列名
- public class Words {
- static class MyColumn implements BaseColumns{
- final static String TABLE_NAME&#61;"dict";
- final static String _ID&#61;"_id";
- final static String WORD&#61;"word";
- final static String DETAIL&#61;"detail";
- }
- }
- package com.example.mysqliitedatabase;
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteDatabase.CursorFactory;
- import android.database.sqlite.SQLiteOpenHelper;
- public class MySqliteDatabaseHelper extends SQLiteOpenHelper {
- String CreateTable&#61;"create table dict"&#43;"("&#43;Words.MyColumn._ID&#43;"integer primary key ,"&#43;Words.MyColumn.WORD&#43;","&#43;Words.MyColumn.DETAIL&#43;")";
- public MySqliteDatabaseHelper(Context context, String name,
- CursorFactory factory, int version) {
- super(context, name, factory, version);
- // TODO Auto-generated constructor stub
- }
- &#64;Override
- public void onCreate(SQLiteDatabase db) {
- // TODO Auto-generated method stub
- db.execSQL(CreateTable);
- }
- &#64;Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- // TODO Auto-generated method stub
- }
- }
- package com.example.mysqliitedatabase;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Stack;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.ContentValues;
- import android.content.Intent;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.text.TextUtils;
- //import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private EditText wordString;
- private EditText detailString;
- private EditText queryString;
- private SQLiteDatabase database;
- private Button addButton;
- private Button queryButton;
- String[] returnColums&#61;{Words.MyColumn.WORD,Words.MyColumn.DETAIL};
- &#64;Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- final MySqliteDatabaseHelper datanaseHelper&#61;new MySqliteDatabaseHelper(getApplicationContext(), "dict.db",null,1);
- wordString&#61;(EditText) findViewById(R.id.editText1);
- detailString&#61;(EditText) findViewById(R.id.editText2);
- queryString&#61;(EditText) findViewById(R.id.editText3);
- addButton&#61; (Button) findViewById(R.id.button1);
- queryButton&#61;(Button) findViewById(R.id.button2);
- addButton.setOnClickListener(new OnClickListener() {
- &#64;Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- final String word&#61;wordString.getText().toString();
- final String detail&#61;detailString.getText().toString();
- database&#61;datanaseHelper.getWritableDatabase();
- ContentValues contentValues&#61;new ContentValues();
- contentValues.put(Words.MyColumn.WORD,word);
- contentValues.put(Words.MyColumn.DETAIL,detail);
- database.insert("dict", null,contentValues);
- Toast.makeText(getApplicationContext(), "add success",1000).show();
- }
- });
- queryButton.setOnClickListener(new OnClickListener() {
- &#64;Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- database&#61;datanaseHelper.getWritableDatabase();
- String query&#61;queryString.getText().toString();
- String[] selectionArgs &#61; null;
- String selection &#61; null;
- Cursor cursor;
- if (!TextUtils.isEmpty(query)) {
- selectionArgs&#61;new String[]{query};
- selection&#61;Words.MyColumn.WORD&#43;"&#61; ?";
- }
- cursor&#61;database.query("dict", returnColums, selection, selectionArgs, null,null,null);
- ArrayList
- if (cursor!&#61;null) {
- while (cursor.moveToNext()) {
- Map
map&#61;new HashMap (); - int wordIndex&#61;cursor.getColumnIndex(Words.MyColumn.WORD);
- int detailIndex&#61;cursor.getColumnIndex(Words.MyColumn.DETAIL);
- map.put(Words.MyColumn.WORD,cursor.getString(wordIndex));
- map.put(Words.MyColumn.DETAIL,cursor.getString(detailIndex));
- list.add(map);
- }
- Toast.makeText(getApplicationContext(), "query success",1000).show();
- Intent intent&#61;new Intent(getApplicationContext(), ResultActivity.class);
- Bundle bundle&#61;new Bundle();
- bundle.putSerializable("data",list);
- intent.putExtras(bundle);
- startActivity(intent);
- }
- else {
- Toast.makeText(getApplicationContext(), "cursor is null",1000).show();
- }
- }
- });
- }
- }
- package com.example.mysqliitedatabase;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- public class ResultActivity extends Activity {
- &#64;Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- ListView listView&#61;new ListView(getApplicationContext());
- setContentView(listView);
- Intent intent&#61;getIntent();
- ArrayList
> list&#61;(ArrayList >) intent.getSerializableExtra("data"); - SimpleAdapter adapter&#61;new SimpleAdapter(getApplicationContext(), list, R.layout.main, new String[]{Words.MyColumn.WORD,Words.MyColumn.DETAIL},new int[]{R.id.text1,R.id.text2});
- listView.setAdapter(adapter);
- }
- }