标签:XML parent tco new source read xml文件 文件 edittext
要求
需要实现的基本功能要求:
1.将学生信息存入数据库
2.显示所有学生信息列表
3.删除数据库表中第一条信息
界面制作
1.先布局xml文件
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/hint" android:id="@+id/shuru" android:textSize="20sp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/add" android:text="@string/add" android:textSize="20sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/delete" android:text="@string/delete" android:textSize="20sp" /> </LinearLayout> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/list_show" android:choiceMode="singleChoice" android:listSelector="#fff0"> </ListView>
string.xml文件
<resources> <string name="app_name">SQLwork</string> <string name="name">姓名</string> <string name="hint">请输入你的姓名</string> <string name="add">增加一条记录</string> <string name="delete">删除第一条记录</string> </resources>
2.创建数据库表
public class MyDBHelper extends SQLiteOpenHelper { private String sql = "create table student(_id integer primary key autoincrement," + "name text not null)";//创建表的sql语句 public MyDBHelper(Context context) { super(context, "studentInfo", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(sql); } //newVersion>oldVersion(旧版本)调用 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("drop table if exists student"); onCreate(db); }
public class Name implements Serializable { private int _id; private String name; public Name(){ } public Name(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 NameDAO { private Context context;//上下文 private MyDBHelper helper;//创建,升级,打开数据库 private SQLiteDatabase db;//对表增删改查 public NameDAO(Context context){ helper=new MyDBHelper(context); } public void insert(Name name){ //打开数据库 db=helper.getWritableDatabase(); //生成数据集合 ContentValues values=new ContentValues(); values.put("name",name.getName()); //执行语句 db.insert("student",null,values); } public Cursor selectAll() { db =helper.getReadableDatabase(); Cursor cursor= db.query("student",null,null,null,null,null,null); return cursor; } public void delete(int id){ db = helper.getWritableDatabase(); db.delete("student","_id=?",new String[]{String.valueOf(id)});//根据数据的id号来删除数据 } }
case R.id.delete: if(name!=null){ //判断记录是否为空,如果是空的话会提示记录为空 cursor.moveToFirst();//数据的第一行 name=new Name(); name.set_id(cursor.getInt(cursor.getColumnIndex("_id")));获取数据第一行的id nameDAO.delete(name.get_id());//调用delete方法删除第一条记录 nameDAO = new NameDAO(this); cursor = nameDAO.selectAll(); adapter = new MyCursorAdapter(this, cursor); lvshow.setAdapter(adapter); Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show(); } Toast.makeText(this,"记录为空",Toast.LENGTH_SHORT).show(); break;
标签:XML parent tco new source read xml文件 文件 edittext
原文地址:http://www.cnblogs.com/xu2829346482/p/6864303.html