java

package com.sxt.day06_10;

import java.util.ArrayList;

import com.sxt.day06_10.entity.StudentBean;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView; public class MainActivity extends Activity {
StudentDBHelper mDao; ListView mlvStudent;
ArrayList<StudentBean> mStudents;
StudentAdapter mAdapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDao=new StudentDBHelper(this);
initData();
initView();
} private void initView() {
mlvStudent=(ListView) findViewById(R.id.lvStudent);
mAdapter=new StudentAdapter(mStudents, this, mDao);
mlvStudent.setAdapter(mAdapter);
} private void initData() {
mStudents=mDao.queryAll();
} class StudentAdapter extends BaseAdapter{
ArrayList<StudentBean> students;
Context context;
StudentDBHelper dao; public StudentAdapter(ArrayList<StudentBean> students, Context context,//Context是Activity的父类
StudentDBHelper dao) {
super();
this.students = students;
this.context = context;
this.dao = dao;
} public void remove(int position){//列表的删除
dao.deleteRecord(students.get(position).getId());
students.remove(position);
notifyDataSetChanged();
} public void add(StudentBean bean){//列表的增加
students.add(bean);
notifyDataSetChanged();
dao.insertRecored(bean);
} public void update(int position,StudentBean bean){
students.set(position, bean);
notifyDataSetChanged();
dao.updateRecord(bean);
} @Override
public int getCount() {
return students.size();
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if(convertView==null){
//缓存第一屏的所有convertView(比如10个)
convertView=View.inflate(context, R.layout.item_student, null);
holder=new ViewHolder();
holder.tvName=(TextView) convertView.findViewById(R.id.tvName);
holder.tvSex=(TextView) convertView.findViewById(R.id.tvSex);
holder.tvBirthday=(TextView) convertView.findViewById(R.id.tvBirthday);
holder.tvHeight=(TextView) convertView.findViewById(R.id.tvHeight);
convertView.setTag(holder);
}else{
//滚屏的时候获取缓存的所有convertView(比如10个)
holder=(ViewHolder) convertView.getTag();
}
//修改成新的
StudentBean bean=students.get(position);
holder.tvName.setText(bean.getName());
holder.tvSex.setText(bean.getSex());
holder.tvBirthday.setText(bean.getBirthday());
holder.tvHeight.setText(bean.getHeight()+"");
return convertView;
}
class ViewHolder{
TextView tvName,tvSex,tvBirthday,tvHeight;
} }
}
package com.sxt.day06_10;

import java.util.ArrayList;

import com.sxt.day06_10.entity.StudentBean;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper; public class StudentDBHelper extends SQLiteOpenHelper { private static final String DB_NAME="students.db";
static final String TABLE_NAME="student";
static final String ID="_id";
static final String NAME="name";
static final String SEX="sex";
static final String BIRTHDAY="birthday";
static final String HEIGHT="height"; public StudentDBHelper(Context context) {
super(context, DB_NAME, null, 1);
} @Override
public void onCreate(SQLiteDatabase db) {
String sql="create table if not exists "+TABLE_NAME+"("
+ID+" integer primary key autoincrement,"
+NAME+" varchar(50),"
+SEX+" varchar(2),"
+BIRTHDAY+" datetext,"
+HEIGHT+" real)";
db.execSQL(sql);
insertRecords(db);
} private void insertRecords(SQLiteDatabase db) {
ContentValues values=new ContentValues();
values.put(NAME, "张飞");
values.put(SEX, "男");
values.put(BIRTHDAY, "1990-5-5");
values.put(HEIGHT, 1.99);
db.insert(TABLE_NAME, null, values); values=new ContentValues();
values.put(NAME, "王菲");
values.put(SEX, "女");
values.put(BIRTHDAY, "1990-8-5");
values.put(HEIGHT, 1.69);
db.insert(TABLE_NAME, null, values); values=new ContentValues();
values.put(NAME, "刘亦菲");
values.put(SEX, "女");
values.put(BIRTHDAY, "1991-5-5");
values.put(HEIGHT, 1.7);
db.insert(TABLE_NAME, null, values); values=new ContentValues();
values.put(NAME, "李菲");
values.put(SEX, "女");
values.put(BIRTHDAY, "1992-5-5");
values.put(HEIGHT, 1.72);
db.insert(TABLE_NAME, null, values); values=new ContentValues();
values.put(NAME, "田菲");
values.put(SEX, "男");
values.put(BIRTHDAY, "1993-5-5");
values.put(HEIGHT, 1.78);
db.insert(TABLE_NAME, null, values);
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub } /**
* 向表中增加一条记录
* @param bean
* @return
*/
public int insertRecored(StudentBean bean){
ContentValues values=new ContentValues();
values.put(NAME, bean.getName());
values.put(SEX, bean.getSex());
values.put(BIRTHDAY, bean.getBirthday());
values.put(HEIGHT, bean.getHeight());
SQLiteDatabase db = getWritableDatabase();//getWritableDatabase()是SQLiteOpenHelper的方法
long count = db.insert(TABLE_NAME, null, values);
return (int) count;//插入的行数
} /**
* 删除指定id的记录
* @param id
* @return
*/
public int deleteRecord(int id){
SQLiteDatabase db = getWritableDatabase();
int count = db.delete(TABLE_NAME, ID+"=?", new String[]{""+id});
return count;//删除的行号
} public int updateRecord(StudentBean bean){
ContentValues values=new ContentValues();
values.put(NAME, bean.getName());
values.put(SEX, bean.getSex());
values.put(BIRTHDAY, bean.getBirthday());
values.put(HEIGHT, bean.getHeight());
SQLiteDatabase db = getWritableDatabase();
int count = db.update(TABLE_NAME, values, ID+"=?", new String[]{bean.getId()+""});//ID+"=?"是条件,new String[]{bean.getId()+""是填充占位符?
return count;
} public ArrayList<StudentBean> queryAll(){
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.query(TABLE_NAME, null, null, null, null, null, null);//全查询,没有条件
ArrayList<StudentBean> students=new ArrayList<StudentBean>();
while(c.moveToNext()){
int id=c.getInt(0);
String name=c.getString(c.getColumnIndex(NAME));
String sex=c.getString(c.getColumnIndex(SEX));
String birthday=c.getString(c.getColumnIndex(BIRTHDAY));
double height=c.getDouble(c.getColumnIndex(HEIGHT));
StudentBean bean=new StudentBean(id, name, sex, birthday, height);
students.add(bean);
}
return students;
} public StudentBean queryRecord(int id){
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.query(TABLE_NAME, null, ID+"=?", new String[]{""+id}, null,null,null);
if(c.moveToNext()){
String name=c.getString(c.getColumnIndex(NAME));
String sex=c.getString(c.getColumnIndex(SEX));
String birthday=c.getString(c.getColumnIndex(BIRTHDAY));
double height=c.getDouble(c.getColumnIndex(HEIGHT));
StudentBean bean=new StudentBean(id, name, sex, birthday, height);
return bean;
}
return null;
}
}

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ListView
android:id="@+id/lvStudent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="5dp"/> </RelativeLayout>

item_student.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="张飞"
android:layout_marginLeft="5dp"/>
<TextView
android:id="@+id/tvSex"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="男"
android:layout_marginLeft="5dp"/>
<TextView
android:id="@+id/tvBirthday"
android:layout_below="@id/tvName"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="1990-5-5"
android:layout_marginLeft="5dp"/>
<TextView
android:id="@+id/tvHeight"
android:layout_below="@id/tvSex"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="1.99米"
android:layout_marginLeft="5dp"/>
</RelativeLayout>

android 44 SQLiteOpenHelper的更多相关文章

  1. Xamarin.Android之SQLiteOpenHelper

    一.前言 在手机中进行网络连接不仅是耗时也是耗电的,而耗电却是致命的.所以我们就需要数据库帮助我们存储离线数据,以便在用户未使用网络的情况下也可以能够使用应用的部分功能,而在需要网络连接的功能上采用提 ...

  2. Android笔记——SQLiteOpenHelper类

    public 抽象类 SQLiteOpenHelper 继承关系 Java.lang.Object android.database.sqlite.SQLiteOpenHelper 类概要 这是一个辅 ...

  3. Xamarin.Android 使用 SQLiteOpenHelper 进行数据库操作

    一.前言 在手机中进行网络连接不仅是耗时也是耗电的,而耗电却是致命的.所以我们就需要数据库帮助我们存储离线数据,以便在用户未使用网络的情况下也可以能够使用应用的部分功能,而在需要网络连接的功能上采用提 ...

  4. Android:SQLiteOpenHelper类(SQLlite数据库操作)详细解析

    前言 SQLite数据库操作在Android开发中非常常用 今天我将带大家全面了解关于SQLite数据库的操作(增.删.查.改) 目录 1. SQLite数据库介绍 SQLite是Android内置的 ...

  5. Android中SQLiteOpenHelper类的onUpgrade方法浅谈

    public abstract void onUpgrade(SQLiteDatabase db,int oldVersion,int new Version) 这个方法在实现时需要重写. onUpg ...

  6. Android 数据库 SQLiteOpenHelper

    public class DbOpenHelper extends SQLiteOpenHelper { private static String name = "test.db" ...

  7. Xamarin.Android开发实践(十四)

    Xamarin.Android之ListView和Adapter 一.前言 如今不管任何应用都能够看到列表的存在,而本章我们将学习如何使用Xamarin去实现它,以及如何使用适配器和自定义适配器(本文 ...

  8. Android代码速查,写给新手的朋友们[转]

    原文地址:http://www.open-open.com/lib/view/open1397286499090.html 0 android 创建按钮 Button button = new But ...

  9. Xamarin.Android开发实践(十三)

    Xamarin.Android之SQLite.NET ORM 一.前言 通过<Xamarin.Android之SQLiteOpenHelper>和<Xamarin.Android之C ...

随机推荐

  1. about Red_Hat_Enterprise_Linux_7

    systemd systemd 是 Linux 的系统和服务管理程序,替换了 Red Hat Enterprise Linux 之前的发行本中使用的 SysV.systemd 与 SysV 和 Lin ...

  2. WebApi学习总结系列第五篇(消息处理管道)

    引言: ASP.NET WebAPI的核心框架是一个消息处理管道,这个管道是一组HttpMessageHandler的有序组合.这是一个双工管道,请求消息从一端流入并依次经过所有HttpMessage ...

  3. Java Learning:并发中的同步锁(synchronized)

    引言 最近一段时间,实验室已经倾巢出动找实习了,博主也凑合了一把,结果有悲有喜,BAT理所应当的跪了,也收到了其他的offer,总的感受是有必要夯实基础啊. 言归正传,最近在看到java多线程的时候, ...

  4. bzoj 1053: [HAOI2007]反素数ant 搜索

    1053: [HAOI2007]反素数ant Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1497  Solved: 821[Submit][Sta ...

  5. C/C++中的内存对齐 C/C++中的内存对齐

    一.什么是内存对齐.为什么需要内存对齐? 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特 定的内存地址 ...

  6. Android ServiceConnection类的onServiceDisconnected(ComponentName name)在什么时候执行

    ServiceConnection类中的两个方法非别在服务连接成功时.不成功时调用.其中onServiceDisconnected()方法在连接正常关闭的情况下是不会被调用的, 该方法只在Servic ...

  7. 14.8.9 Clustered and Secondary Indexes

    14.8.9 Clustered and Secondary Indexes 每个InnoDB 表有一个特殊的索引称为 clustered index 用于存储数据. 通常, clustered in ...

  8. 【HDOJ】1455 Sticks

    DFS.搜索以棍数为条件循环搜索较好,这样不会超时. #include <stdio.h> #include <string.h> #include <stdlib.h& ...

  9. Linux Shell编程(13)——数字常量

    除非一个数字有特别的前缀或符号,否则shell脚本把它当成十进制的数.一个前缀为0的数字是八进制数.一个前缀为0x的数字是十六进制数.一个数用内嵌的#来求值则看成BASE#NUMBER(有范围和符号限 ...

  10. HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))

    Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...