SQLiteOpenHelper使用之英文生词本
来源:互联网 发布:excel怎么数据透视 编辑:程序博客网 时间:2024/04/29 10:28
界面效果如下:
布局代码如下:
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout 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" tools:context="com.example.sqliteopenhelper.MainActivity" android:orientation="vertical"> <EditText android:id="@+id/et_word" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="input the word"/> <EditText android:id="@+id/et_explaination" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入单词解释 "/> <Button android:id="@+id/btn_insert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="insert"/> <EditText android:id="@+id/et_search_content" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="search"/></LinearLayout></span>第一步:创建一个MyDataBaseHelper类,继承SQLiteOpenHelper类,此类用于建立数据库(表),代码如下:
<span style="font-size:14px;">public class MyDataBaseHelper extends SQLiteOpenHelper{ final String CREATE_TABLE_SQL = "create table dict(_id integer primary key autoincrement," + "word,detail)"; public MyDataBaseHelper(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) { System.out.println("onUpgrade Called-------oldVersion:"+ oldVersion+"newVersion:"+newVersion); }}</span>MainActivity代码如下:
<span style="font-size:14px;">public class MainActivity extends Activity { private MyDataBaseHelper helper; private Button btn_insert,btn_search; private EditText et_word,et_explanation,searchContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); helper = new MyDataBaseHelper(this,"myDictionary.db3",null,1); btn_insert = (Button) findViewById(R.id.btn_insert); btn_search = (Button) findViewById(R.id.btn_search); et_word = (EditText) findViewById(R.id.et_word); searchContent = (EditText) findViewById(R.id.et_search_content); et_explanation = (EditText) findViewById(R.id.et_explaination); btn_insert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String word = et_word.getText().toString(); String detail = et_explanation.getText().toString(); // helper.getReadableDataBase是用读写的方式打开数据库 insertData(helper.getReadableDatabase(),word,detail); Toast.makeText(MainActivity.this, "添加单词成功!", Toast.LENGTH_SHORT).show(); } }); btn_search.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String key = searchContent.getText().toString(); Cursor cursor = helper.getReadableDatabase().rawQuery("select * from dict where word " + "like ? or detail like ?",new String[]{"%"+key+"%","%"+key+"%"}); Bundle data = new Bundle(); data.putSerializable("data", (Serializable) converCursorToList(cursor)); Intent intent = new Intent(MainActivity.this,ResultActivity.class); intent.putExtras(data); startActivity(intent); } }); } private void insertData(SQLiteDatabase db,String word,String detail){ db.execSQL("insert into dict values(null,?,?)",new String[]{word,detail}); } private List<Map<String,String>> converCursorToList(Cursor cursor){ ArrayList<Map<String,String>> wordList = new ArrayList<Map<String,String>>(); while(cursor.moveToNext()){ Map<String,String> map = new HashMap<String,String>(); map.put("word",cursor.getString(1)); map.put("detail",cursor.getString(2)); wordList.add(map); } return wordList; }}</span><span style="font-size: 12pt;"></span>ResultActivity代码如下:
<span style="font-size:14px;">public class ResultActivity extends Activity { ListView searchedList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.resultlist); searchedList = (ListView) findViewById(R.id.searched_list); Intent intent = getIntent(); Bundle data = intent.getExtras(); ArrayList<Map<String,String>> list =(ArrayList<Map<String,String>>) data.getSerializable("data"); SimpleAdapter adapter = new SimpleAdapter(ResultActivity.this,list, R.layout.result, new String[]{"word","detail"},new int[]{R.id.tv_word,R.id.tv_detail}); searchedList.setAdapter(adapter); }}</span>注:将ResultActivity设置为对话框模式的Activity,在AndroidManifest.xml中设置:<activity android:name=".ResultActivity" android:theme="@style/Theme.AppCompat.Dialog"></activity>在ResultActivity中需要再新建2个xml布局文件resultlist.xml和result.xmlSimpleAdapter adapter = new SimpleAdapter(context,List<Map<String,?>>,int resource,String from[],int to[]);其中resource是要填充的布局文件,from[]对应list中存入的key,to[]对应布局文件中的组件的id,切记使用listView.setAdapter(adapeter)方法使listView按resource中的样式加载list中的数据
界面查询效果如下:
0 0
- SQLiteOpenHelper使用之英文生词本
- SQLiteOpenHelper类实例-英文生词本
- SQLiteOpenHelper生词本
- 生词本
- 生词本
- 生词本
- Android之使用SQLiteOpenHelper
- java tutorial生词本
- 批处理将生词导入金山词霸生词本!
- Android之SQLiteOpenHelper的使用
- iPhone/iPad高级应用与手游开发学习笔记:sqlite3学习(简单英文生词本demo)
- 阅读英文文档遇到的生词
- Android之Sqlite,SQLiteOpenHelper的使用
- 使用SQLiteOpenHelper
- 使用SQLiteOpenHelper
- SQLiteOpenHelper 使用
- LingosHook:Lingoes生词本下载页
- 让灵格斯有生词本的功能 LingosHook
- session实现带随机验证码的登陆
- javaScript 冒泡
- 1103. Integer Factorization (30)-PAT甲级真题(dfs深度优先)
- 贪心算法求解问题的选择准则
- Sql Server 中的架构用户登录和角色
- SQLiteOpenHelper使用之英文生词本
- JProfiler 9.2
- ovs bond选项情况
- 安装 Android Studio --- Linux
- TCP/IP详解卷一 第一章
- python正则表达式对象和函数
- 《C 程序设计语言》 第八章 malloc 函数实现于内测管理
- 判断ie版本
- 7.4 B SCU 3596Article Decryption