数据库
首先依旧是基本的布局
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.shujukuapplication.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20sp"
android:id="@+id/xm" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加一条新记录"
android:id="@+id/tj"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除第一条记录"
android:id="@+id/sc"
android:onClick="onClick" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/view" >
</ListView>
</LinearLayout>
自定义
public class Adapter extends CursorAdapter {
public Adapter(MainActivity context, Cursor cursor)
{
super(context, cursor,0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.item_student,parent,false);
}
@Override
public void bindView(View view, Context context, Cursor cursor)
{
TextView name=(TextView)view.findViewById(R.id.xm);
name.setText(cursor.getString(cursor.getColumnIndex("name")));
}
}
创建数据库
public class Helper extends SQLiteOpenHelper
{
private String sql="create table student1(_id integer primary key autoincrement," + "name text not null)";
public Helper(Context context){ super(context,"student1Info", 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 student1");
onCreate(db)
}
}
public class Student { private int _id; private String name; public Student()
{
}
public Student(String name)
{
this.name = name;
}
public int get_Id() { return _id;
}
public void set_Id(int _id) { this._id = _id;
}
public String getName() { return name;
}
public void setName(String name) { this.name = name;
}
}
public class StudentDAO
{
private Context context;
private Helper helper;
private SQLiteDatabase db;
public StudentDAO(Context context)
{
helper=new Helper(context);
}
public void insert(Student student)
{
db=helper.getWritableDatabase();
String sql = "insert into student1(name)"+"values(?)";
db.execSQL(sql, new Object[]{ student.getName()} );
}
public Cursor selectAll()
{
db = helper.getReadableDatabase();
Cursor cursor = db.query("student1", null, null, null, null, null, null); return cursor;
}
public void delete(int id)
{
String sql = "delete from student1 where _id=?";
db.execSQL(sql, new Object[]{id});
}
}
控件的获取
xm=(EditText)findViewById(R.id.xm);
view=(ListView)findViewById(R.id.view);
添加按钮
public void onClick(View v)
{
String name=xm.getText().toString();
switch
(v.getId())
{
case R.id.tj:
studentDAO=new StudentDAO(this);
student=new Student(name);
studentDAO.insert(student);
studentDAO =new StudentDAO(this);
cursor=studentDAO.selectAll();
adapter = new Adapter(this,cursor);
view.setAdapter(adapter);