Android+SQLite数据库实现的生词记事本功能实例

Android+SQLite数据库实现的生词记事本功能实例,第1张

概述本文实例讲述了Android+SQLite数据库实现的生词记事本功能。分享给大家供大家参考,具体如下:

本文实例讲述了AndroID+sqlite数据库实现的生词记事本功能。分享给大家供大家参考,具体如下:

主activity命名为

Dict:

代码如下:

package example.com.myapplication;import androID.app.Activity;import androID.content.Intent;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Toast;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;public class Dict extends Activity{  MyDatabaseHelper dbHelper;  button insert = null;  button search = null;  @OverrIDe  public voID onCreate(Bundle savedInstanceState)  {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    // 创建MyDatabaseHelper对象,指定数据库版本为1,此处使用相对路径即可,    // 数据库文件自动会保存在程序的数据文件夹的databases目录下。    dbHelper = new MyDatabaseHelper(this,"myDict.db3",1);    insert = (button)findVIEwByID(R.ID.insert);    search = (button)findVIEwByID(R.ID.search);    insert.setonClickListener(new VIEw.OnClickListener()    {      @OverrIDe      public voID onClick(VIEw source)      {        //获取用户输入        String word = ((EditText)findVIEwByID(R.ID.word))            .getText().toString();        String detail = ((EditText)findVIEwByID(R.ID.detail))            .getText().toString();        //插入生词记录        insertData(dbHelper.getReadableDatabase(),word,detail);        //显示提示信息        Toast.makeText(Dict.this,"添加生词成功!",Toast.LENGTH_SHORT)            .show();      }    });    search.setonClickListener(new VIEw.OnClickListener()    {      @OverrIDe      public voID onClick(VIEw source)      {        // 获取用户输入        String key = ((EditText) findVIEwByID(R.ID.key)).getText()            .toString();        // 执行查询        Cursor cursor = dbHelper.getReadableDatabase().rawquery(            "select * from dict where word like ? or detail like ?",new String[]{"%" + key + "%","%" + key + "%"});        //创建一个Bundle对象        Bundle data = new Bundle();        data.putSerializable("data",converCursorToList(cursor));        //创建一个Intent        Intent intent = new Intent(Dict.this,ResultActivity.class);        intent.putExtras(data);        //启动Activity        startActivity(intent);      }    });  }  protected ArrayList<Map<String,String>>  converCursorToList(Cursor cursor)  {    ArrayList<Map<String,String>> result =      new ArrayList<Map<String,String>>();    //遍历Cursor结果集    while(cursor.movetoNext())    {      //将结果集中的数据存入ArrayList中      Map<String,String> map = new          HashMap<String,String>();      //取出查询记录中第2列、第3列的值      map.put("word",cursor.getString(1));      map.put("detail",cursor.getString(2));      result.add(map);    }    return result;  }  private voID insertData(sqliteDatabase db,String word,String detail)  {    //执行插入语句    db.execsql("insert into dict values(null,?,?)",new String[]{word,detail});  }  @OverrIDe  public voID onDestroy()  {    super.onDestroy();    //退出程序时关闭MyDatabaseHelper里的sqliteDatabase    if (dbHelper != null)    {      dbHelper.close();    }  }}

他的布局文件activity_main代码如下:

<!--?xml version="1.0" enCoding="utf-8"?--><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:layout_wIDth="fill_parent"  androID:layout_height="fill_parent"  androID:orIEntation="vertical">  <EditText    androID:ID="@+ID/word"    androID:layout_wIDth="fill_parent"    androID:layout_height="wrap_content"    androID:hint="@string/input"/>  <EditText    androID:ID="@+ID/detail"    androID:layout_wIDth="fill_parent"    androID:layout_height="wrap_content"    androID:hint="@string/input"    androID:lines="3"/>  <button    androID:ID="@+ID/insert"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:text="@string/insert"/>  <EditText    androID:ID="@+ID/key"    androID:layout_wIDth="fill_parent"    androID:layout_height="wrap_content"    androID:hint="@string/record"/>  <button    androID:ID="@+ID/search"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:text="@string/search"/>  <ListVIEw    androID:ID="@+ID/show"    androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent"/></linearLayout>

另一个需要跳转的activity命名为:

ResultActivity

具体代码如下:

package example.com.myapplication;import androID.app.Activity;import androID.content.Intent;import androID.os.Bundle;import androID.Widget.ListVIEw;import androID.Widget.SimpleAdapter;import java.util.List;import java.util.Map;public class ResultActivity extends Activity{  @OverrIDe  public voID onCreate(Bundle savedInstanceState)  {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.popup);    ListVIEw ListVIEw = (ListVIEw)findVIEwByID(R.ID.show);    Intent intent = getIntent();    //获取该intent所携带的数据    Bundle data = intent.getExtras();    //从Bundle数据包中取出数据    @SuppressWarnings("unchecked")    List<Map<String,String>> List =      (List<Map<String,String>>)data.getSerializable("data");    //将List封装成SimpleAdapter    SimpleAdapter adapter = new SimpleAdapter(        ResultActivity.this,List,R.layout.ine,new String[]{"word","detail"},new int[]{R.ID.my_Title,R.ID.my_content});    //填充ListVIEw    ListVIEw.setAdapter(adapter);  }}

他的布局文件命名为popup: 代码如下:

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent"  androID:ID="@+ID/fragment">  <TextVIEw    androID:ID="@+ID/my_Title"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content" />  <TextVIEw    androID:ID="@+ID/my_content"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content" /></linearLayout>

ListVIEw的子项目布局命名为ine:

代码如下:

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent"  androID:ID="@+ID/fragment">  <TextVIEw    androID:ID="@+ID/my_Title"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content" />  <TextVIEw    androID:ID="@+ID/my_content"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content" /></linearLayout>

最后数据库帮助类命名为:

MyDatabaseHelper:

代码如下:

package example.com.myapplication;import androID.content.Context;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;public class MyDatabaseHelper extends sqliteOpenHelper{  final String CREATE_table_sql =      "create table dict(_ID integer primary key autoincrement,detail)";  public MyDatabaseHelper(Context context,String name,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("--------onUpdate Called--------"        + oldVersion + "--->" + newVersion);  }}

更多关于AndroID相关内容感兴趣的读者可查看本站专题:《Android *** 作SQLite数据库技巧总结》、《Android数据库 *** 作技巧总结》、《Android编程之activity *** 作技巧总结》、《Android文件 *** 作技巧汇总》、《Android开发入门与进阶教程》、《Android资源 *** 作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结》

希望本文所述对大家AndroID程序设计有所帮助。

总结

以上是内存溢出为你收集整理的Android+SQLite数据库实现的生词记事本功能实例全部内容,希望文章能够帮你解决Android+SQLite数据库实现的生词记事本功能实例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1144415.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存