SQLite—homework

主要的界面布局:

 <EditText
android:id="@+id/edt"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/add"
android:textSize="10sp"
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="onClick"
android:layout_height="wrap_content"
android:text="增加一条新记录"/>
<Button
android:id="@+id/delete"
android:textSize="10sp"
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="onClick"
android:layout_height="wrap_content"
android:text="删除第一条记录"/>
</LinearLayout>
<ListView
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content">

              

第二个布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_list"/>

</LinearLayout>
首先要进行数据库表的创建:
跟之前的一样 只是没那么复杂
public class MyDBHelper extends SQLiteOpenHelper {
private String sql = "create table student(_id integer primary key autoincrement," + "name text not null," +
"classmate text not null,"+"age integer)";


public MyDBHelper(Context context) {
super(context, "studentInfo", null, 1);

}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(sql);
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists student");
onCreate(db);
}
}



DAO部分
public class DAO {
private MyDBHelper dbHelper;
private SQLiteDatabase sqLiteDatabase;

public DAO(Context context){
dbHelper = new MyDBHelper(context);
}
//向数据库中插入数据
public void insert(String name){
sqLiteDatabase = dbHelper.getWritableDatabase();
String sql = "insert into person(name) values('"+name+"')";
sqLiteDatabase.execSQL(sql);
}

public Cursor selectAll() {
sqLiteDatabase = dbHelper.getReadableDatabase();
Cursor cursor = sqLiteDatabase.query("person", null, null, null, null, null, null);
return cursor;
}

public void delete(String id){
sqLiteDatabase = dbHelper.getWritableDatabase();
String sql = "delete from person where _id="+String.valueOf(id);//"delete from student where _id="+id
sqLiteDatabase.execSQL(sql);
}
}
Adapter部分:
public class MyCursorAdapter extends CursorAdapter {


public MyCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
return LayoutInflater.from(context).inflate(R.layout.listview_mian,viewGroup,false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView name = (TextView) view.findViewById(R.id.tv_list);
name.setText(cursor.getString(cursor.getColumnIndex("name")));
}
}
MainActivity Java代码:
public class MainActivity extends AppCompatActivity {
private EditText edt;
private ListView listView;
private MyCursorAdapter adapter;
private DAO DAO;
private Cursor cursor;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

edt = (EditText)findViewById(R.id.edt);

listView = (ListView)findViewById(R.id.list_item);
DAO = new DAO(this);
cursor = DAO.selectAll();
if(cursor!=null){
adapter = new MyCursorAdapter(MainActivity.this,cursor);
listView.setAdapter(adapter);
}


}

public void onClick(View view) {
switch(view.getId()){
case R.id.add:

String name = edt.getText().toString();
DAO.insert(name);
cursor = DAO.selectAll();
if(cursor!=null){
adapter = new MyCursorAdapter(this,cursor);
listView.setAdapter(adapter);
}
break;
case R.id.delete:

if(cursor!=null){
if(cursor.moveToFirst()){
DAO.delete(cursor.getString(cursor.getColumnIndex("_id")));
cursor = DAO.selectAll();
}
}
adapter = new MyCursorAdapter(this,cursor);
listView.setAdapter(adapter);
break;
}

}
}

但不幸的是 运行起来依然存在问题 。。。

 


 
 

转载于:https://www.cnblogs.com/liyuan1988/p/6864186.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/269389.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机硬件假故障,计算机硬件故障

计算机硬件故障电脑故障是指造成电脑系统正常工作能力失常的硬件物理损坏和软件系统的错误&#xff0c;因此总的可以分为硬件故障和软件故障&#xff0e;&#xff11;&#xff0e;硬件故障硬件故障是指电脑硬件系统使用不当或硬件物理损坏所造成的故障&#xff0e;例如&#xf…

@EnableConfigurationProperties 注解和@ConfigurationProperties注解实现配置绑定

ConfigurationProperties注解主要用来把properties配置文件转化为bean来使用的&#xff0c;而EnableConfigurationProperties注解的作用是ConfigurationProperties注解生效。如果只配置ConfigurationProperties注解&#xff0c;在IOC容器中是获取不到properties配置文件转化的b…

Windows Server 2003 群集配置测试

安装后配置心跳配置现在&#xff0c;每个节点上的网络都已得到了正确的配置&#xff0c;而且群集服务也已配置完毕&#xff0c;这时您需要配置网络角色&#xff0c;以定义其在群集中的功能。此处是一份“群集管理器”中网络配置选项的列表&#xff1a;为群集应用启用&#xff1…

可穿戴计算机硬件技术研究,可穿戴计算机硬件技术应用探究.doc

可穿戴计算机硬件技术应用探究可穿戴计算机硬件技术应用探究摘 要 可穿戴计算机是科技进步的产物&#xff0c;结合了“以人为本”的理念&#xff0c;作为新一代的计算机&#xff0c;其拥有独具一格的特征和性能。本文介绍可穿戴计算机的概念和特点&#xff0c;简要分析可穿戴计…

html.1

从w3school开始我的征途转载于:https://www.cnblogs.com/yueruifeng/p/6868836.html

“http 500内部服务器错误”的解决方法

“http 500内部服务器错误”的解决方法 IIS500错误&#xff0c;是因为微软的一个BUG所造成的。下面是解决办法:主要是由于IWAM账号&#xff08;在我的计算机即是IWAM_MYSERVER账号&#xff09;的密码错误造成了HTTP 500内部错误。 在详细分析HTTP500内部错误产生的原因之前&…

debug=true开启自动配置报告

Negative matches:表示不生效的自动配置 Positive matches:表示生效的自动配置

计算机的键盘如何保养,知识每天涨一点:快捷键2 键盘键位知识 电脑小保养

快捷键2win菜单键 L 快速锁屏&#xff0c;如果加了密码的话就可以在自己离开的时候防止别人窥屏哦win菜单键 E 快速打开我的电脑&#xff0c;加快查找文件的速度win菜单键 R 在打开的运行页面中输入psr.exe 回车进入选择开始录制就可以录制电脑此时操作了(不是视频&#xff0c;…

强制IE下载的代码

强制ie下载代码&#xff1a;示例&#xff1a;下载source/aaa.zip文件<a href"download.asp?naaa.zip">download.asp: <%Response.Buffer trueResponse.Cleardim urlDim fso,fl,flsizedim DnameDim objStream,ContentType,flName,isre,url1’*************…

vue移动端300毫秒延时

引入&#xff0c;在main.js里 import FastClick from fastclick FastClick.attach(document.body); 双击事件另行封装转载于:https://www.cnblogs.com/liuhuanwen/p/6869096.html

Lombok中@Data注解 @ToString注解 @NoArgsConstructo注解 @AllArgsConstructor注解

1:项目中引入依赖 <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency> 2:实体类上加上Data注解可以生产get和set方法 ToString表示可以生成ToString方法 NoArgsConstructor表示无参构造器 …

数据库计算机报告,外文数据库计算机检索报告实例.pdf

计算机检索报告姓名___________ 班级__________ 学号__________检 索 课 题&#xff1a; 跨文化组织行为研究数 据 库&#xff1a; EI village检 索 步 骤&#xff1a;1、确定检索提问式主题检索&#xff1a;Subject/Title/Abstract “culture cross”ANDSubject/Title/Abstrac…

MSSQL DBA权限获取WEBSHELL的过程

前言 本文主要通过一个案例来演示一下当MSSQL是DBA权限&#xff0c;且不知道路径的时候如何去获取WEBSHELL。当然这种方式对站库分离的无效。我测试的环境是在Win7 64位下&#xff0c;数据库是SQLServer 2000&#xff0c;IIS版本是7.5&#xff0c;程序是采用风讯的CMS。后台登录…

用编码实现gridview的创建!(一)(转www.asp.net)

完全用编码实现gridview创建和clounms的添加。 原理大概是三部分&#xff1a; datasource对象的设置和gridview对象的设置和Columns的添加。看完后感觉不难&#xff0c;但是人家的思路的确值得我们推敲。 这是第一部分&#xff0c;等第二部分出来了&#xff0c;马上更新。 prot…

计算机报名显示事务已被锁死,ORA-01591错误处理: 锁定已被有问题的分配事务处理20.18.156406挂起(转载)...

现象&#xff1a;dms中几乎所有的入库出库都报错2005-12-10 09:12:53 [ERROR] com.topup.ent.kroraina.model.sysmgmt.usersqlquery.USqlQueryDAO (USqlQueryDAO.java :4255) - errorjava.sql.SQLException: ORA-01591: 锁定已被有问题的分配事务处理20.18.156406挂…

系统进程和服务全集

系统进程和服务全集一、最基本的系统进程&#xff08;也就是说&#xff0c;这些进程是系统运行的基本条件&#xff0c;有了这些进程&#xff0c;系统就能正常运行&#xff09;smss.exe Session Managercsrss.exe 子系统服务器进程winlogon.exe 管理用户登录 services.exe 包含很…

筛选染色体连续区域的策略

要求&#xff1a;筛选染色体上符合要求的连续的纯合突变区域。perl (/(ㄒoㄒ)/~~现在只会用perl) 笨办法&#xff0c;正则匹配&#xff1a; 举例子1&#xff1a; my $info(HOMA,871334,HET,874950,HOMA,876499,HOMA,887643); while($info ~ /((HOMA,(\d),){2,})/g){ pr…

macpro连接不到索尼耳机WH-1000XM3搜索不到索尼连接

这个苹果电脑应该不是你耳机连接的第一个设备&#xff0c;这样的话将耳机在关机状态下长按七秒左右&#xff0c;直到指示灯显示红蓝色闪烁&#xff0c;然后再次尝试连接蓝牙就可以了&#xff0c;这个其实是耳机为了防止连错其他设备而做的设置&#xff0c;我们将这个状态重置了…

最新服务器处理器天梯,2019服务器CPU天梯图之性能排行榜(双路三路四路) - 服务器百科...

排名多路处理器CPU性能分1[四路] Intel Xeon Platinum 8180 2.50GHz441392[双路] Intel Xeon Gold 6154 3.00GHz359483[双路] Intel Xeon Gold 6138 2.00GHz356594[双路] Intel Xeon Gold 6152 2.10GHz353685[双路] Intel Xeon E5-2699 v4 2.20GHz343306[双路] Intel Xeon…