热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

AndroidSQLite数据库的使用实例详解

2019独角兽企业重金招聘Python工程师标准创建数据库帮助类DbHelper.java:packagecom.example.db;importandroid.conte


2019独角兽企业重金招聘Python工程师标准>>> hot3.png

创建数据库帮助类DbHelper.java:

package com.example.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table if not exists tb_people"+
"(_id integer primary key autoincrement,"+
"name varchar(20),"+
"phone varchar(12),"+
"mobile varchar(12),"+
"email varchar(30))");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}


主布局文件:

ch9_sqlmain.xml


android:layout_
android:layout_
android:orientation="vertical" >
android:layout_
android:text="所有联系人:"
android:textSize="15px"/>
android:layout_
android:layout_>
android:layout_
android:text="编号"/>
android:layout_
android:text="姓名"/>
android:layout_
android:text="电话"/>
android:layout_
android:text="手机"/>
android:layout_
android:text="电子信箱"/>

android:layout_
android:id="@+id/list_people"/>


编写ListView的Item显示布局文件ch9_peoplelist.xml:


android:layout_
android:layout_
android:orientation="horizontal" >
android:layout_
android:layout_/>
android:layout_
android:layout_/>
android:layout_
android:layout_/>
android:layout_
android:layout_/>
android:layout_
android:layout_/>



主活动类SqlMainActivity.java:

package com.example.ch9;
import com.example.baseexample.R;
import com.example.db.DbHelper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class SqlMainActivity extends Activity {
private ListView list_people;
private DbHelper dbhelper;
private SQLiteDatabase db;

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.ch9_sqlmain);

list_people = (ListView)findViewById(R.id.list_people);

dbhelper = new DbHelper(this, "Db_People", null, 1);

db = dbhelper.getReadableDatabase();
Cursor c = db.query("tb_people", new String[]{"_id","name","phone","mobile","email"}, null, null, null, null, null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.ch9_peoplelist, c, new String[]{"_id","name","phone","mobile","email"}, new int[]{R.id.id,R.id.name,R.id.phone,R.id.mobile,R.id.email});

this.list_people.setAdapter(adapter);
this.registerForContextMenu(list_people);
}

public boolean onCreateOptionsMenu(Menu menu){
menu.add(Menu.NONE,Menu.FIRST+1,1,"添加").setIcon(android.R.drawable.ic_menu_add);
menu.add(Menu.NONE,Menu.FIRST+1,2,"退出").setIcon(android.R.drawable.ic_menu_delete);
return true;
}

public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case Menu.FIRST+1:
Intent intent = new Intent();
intent.setClass(SqlMainActivity.this, AddPeopleActivity.class);
startActivity(intent);
break;
case Menu.FIRST+2:finish();
break;
}
return super.onOptionsItemSelected(item);
}

public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo){
menu.setHeaderIcon(R.drawable.ic_launcher);
menu.add(0,3,0,"修改");
menu.add(0, 4, 0, "删除");
}

public boolean onContextItemSelected(MenuItem item){
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo)item.getMenuInfo();
switch(item.getItemId()){
case 3:
String name=((TextView)menuInfo.targetView.findViewById(R.id.name)).getText().toString();
String phOne=((TextView)menuInfo.targetView.findViewById(R.id.phone)).getText().toString();
String mobile=((TextView)menuInfo.targetView.findViewById(R.id.mobile)).getText().toString();
String email=((TextView)menuInfo.targetView.findViewById(R.id.email)).getText().toString();
Intent intent = new Intent();
intent.setClass(SqlMainActivity.this, AddPeopleActivity.class);
Bundle bundle = new Bundle();
bundle.putLong("id", menuInfo.id);
bundle.putString("name", name);
bundle.putString("phone", phone);
bundle.putString("mobile", mobile);
bundle.putString("email", email);
intent.putExtras(bundle);
startActivity(intent);
break;
case 4:
dbhelper = new DbHelper(this,"Db_People",null,1);
db = dbhelper.getWritableDatabase();
db.delete("tb_people", "_id=?", new String[]{menuInfo.id+""});
break;
}

return true;
}

}


布局文件ch9_addpeople.xml:


android:layout_
android:layout_
android:orientation="vertical" >
android:layout_
android:orientation="horizontal">
android:layout_
android:text="用户名"
android:layout_weight="2"/>
android:layout_
android:id="@+id/edt_name"
android:layout_weight="1"/>

android:layout_
android:orientation="horizontal">
android:layout_
android:text="联系电话"
android:layout_weight="2"/>
android:layout_
android:id="@+id/edt_phone"
android:layout_weight="1"/>

android:layout_
android:orientation="horizontal">
android:layout_
android:text="手机"
android:layout_weight="2"/>
android:layout_
android:id="@+id/edt_mobile"
android:layout_weight="1"/>

android:layout_
android:orientation="horizontal">
android:layout_
android:text="电子信箱"
android:layout_weight="2"/>
android:layout_
android:id="@+id/edt_email"
android:layout_weight="1"/>

android:layout_
android:orientation="horizontal">

推荐阅读
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Java String与StringBuffer的区别及其应用场景
    本文主要介绍了Java中String和StringBuffer的区别,String是不可变的,而StringBuffer是可变的。StringBuffer在进行字符串处理时不生成新的对象,内存使用上要优于String类。因此,在需要频繁对字符串进行修改的情况下,使用StringBuffer更加适合。同时,文章还介绍了String和StringBuffer的应用场景。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • vue使用
    关键词: ... [详细]
  • 一、Hadoop来历Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • PHP中的单例模式与静态变量的区别及使用方法
    本文介绍了PHP中的单例模式与静态变量的区别及使用方法。在PHP中,静态变量的存活周期仅仅是每次PHP的会话周期,与Java、C++不同。静态变量在PHP中的作用域仅限于当前文件内,在函数或类中可以传递变量。本文还通过示例代码解释了静态变量在函数和类中的使用方法,并说明了静态变量的生命周期与结构体的生命周期相关联。同时,本文还介绍了静态变量在类中的使用方法,并通过示例代码展示了如何在类中使用静态变量。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
author-avatar
平凡洗护店
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有