標籤(空格分隔): Androidjavascript
-任務要求
1 將學生信息存入數據庫
2 顯示全部學生信息列表
3 刪除數據庫表中第一條信息。
如圖所示:
java
思路:android
-刪除數據庫表中的第一條數據
刪除的話經過數據庫語句delete語句實現 用cursor來定位第一條數據來實現刪除sql
XMl佈局:
main_activity數據庫
<EditText android:id="@+id/edt_cont" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_weight="1" android:id="@+id/btn_add" android:layout_width="0dp" android:layout_height="wrap_content" style="@style/add"/> <Button android:layout_weight="1" android:id="@+id/btn_del" android:layout_width="0dp" android:layout_height="wrap_content" style="@style/del"/> </LinearLayout> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"/>
item後端
<TextView android:id="@+id/txt_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=""/>
在數據庫建立部分須要一個SQLiteOpenHelper打開SQLite並建立表,因此我自定義了一個MyDBHelper類繼承它ide
-MyDBHelper類佈局
public class MyDBHelper extends SQLiteOpenHelper { private static final String name = "count"; private static final int version = 1; private String sql = "create table person(_id int autoincrement primary key ," + "name varchar(30) not null)"; public MyDBHelper(Context context) { super(context, name, null, version); } @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 person"); onCreate(db); } }
同時須要一個類對錶進行增刪該查,這些操做要在具體的實例上也就表的行或者列進行 所以還須要一個類來表示表的行或者列。這裏我是對行進行增刪改查this
public class PersonDAO { private SQLiteDatabase db; private MyDBHelper helper; public PersonDAO(Context context) {helper = new MyDBHelper(context);} public void insert(Person person) { db = helper.getWritableDatabase(); String sql = "insert into person(name)"+ "values(?)"; db.execSQL(sql,new Object[]{person.getName()}); } public void delete(int id) { db = helper.getWritableDatabase(); String sql = "delete from person where _id =?"; db.execSQL(sql,new Object[]{id}); } public Cursor selectAll() { db = helper.getReadableDatabase(); Cursor cursor = db.query("person",null,null,null,null,null,null); return cursor; } }
Person類(表的每一行)code
public class Person { private String name; private int _id; public Person() {} public Person(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int get_id() { return _id; } public void set_id(int _id) { this._id = _id; } }
在將數據導入界面時還須要用到CursorAdpter,我自定義了一個CursorAdpter
MyCursorAdpter類
public class MyCursorAdpter extends CursorAdapter { public MyCursorAdpter(Context context, Cursor c, int flags) { super(context, c, 0); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return LayoutInflater.from(context).inflate(R.layout.item,parent,false); } @Override public void bindView(View view, Context context, Cursor cursor) { TextView txt_name = (TextView)view.findViewById(R.id.txt_name); txt_name.setText(cursor.getString(cursor.getColumnIndex("name"))); } }
而後是在Mainactivity實現功能的代碼
獲取控件並對事件實施監聽
private Button add; private Button del; private EditText con; private ListView lv; private PersonDAO dao; private Cursor c; private Person person; private MyCursorAdpter adp; add = (Button)findViewById(R.id.btn_add); del = (Button)findViewById(R.id.btn_del); con = (EditText)findViewById(R.id.edt_cont); lv = (ListView)findViewById(R.id.lv); add.setOnClickListener(this); del.setOnClickListener(this);
增長學生信息
String name =con.getText().toString(); if(TextUtils.isEmpty(name)){ Toast.makeText(this,"內容不能爲空",Toast.LENGTH_SHORT).show(); }else { person = new Person(name); dao = new PersonDAO(this); dao.insert(person); c = dao.selectAll(); adp = new MyCursorAdpter(this,c,0); lv.setAdapter(adp); adp.notifyDataSetChanged();
刪除第一條記錄
if(person==null){ Toast.makeText(this,"記錄不能爲空",Toast.LENGTH_SHORT).show(); }else { c.moveToFirst(); person = new Person(); person.set_id(c.getInt(c.getColumnIndex("_id"))); dao.delete(person.get_id()); addAdp(); adp.notifyDataSetChanged(); break; } }
addAdp()方法
private void addAdp() { if(c!=null){ dao = new PersonDAO(this); c = dao.selectAll(); adp = new MyCursorAdpter(this,c,0); lv.setAdapter(adp); } }
圖片成果展現