sql

界面的布局

*很普通的一个布局

<?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">
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/wuyue16"/>
    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <Button
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="40dp"
            android:text="@string/add"/>
        <Button
            android:id="@+id/delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/delete"/>
    </LinearLayout>
<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listview">
</ListView>
    </LinearLayout>

代码功能的实现

public class DiaryDbAdapter {

 
private DatabaseHelper databaseHelper;
private Context context;
private SQLiteDatabase sqliteDatabase;
public DiaryDbAdapter(Context context)
{
this.context = context;
}
/**
数据库创建
db=new dbHelper(testDbActivity.this);//定义数据库辅助类

/**
* 关闭数据库连接
*/
public void close()
{
sqliteDatabase.close();
}
在databaseHelper类中,添加以下3个方法,以便于database调用实现数据的增删改。
/**
* 向数据库表中插入一条数据
*/
public long insert(String title, String Title)
{
SQLiteDatabase db=this.getWritableDatabase();
  ContentValues cv=new ContentValues();
  cv.put(FIELD_TITLE, Title);
  long row=db.insert(TABLE_NAME, null, cv);
  return row;
}
/**
* 删除表中符合条件的记录
*/
public void delete(int id)
  {
  SQLiteDatabase db=this.getWritableDatabase();
  String where=FIELD_ID+"=?";
  String[] whereValue={Integer.toString(id)};
  db.delete(TABLE_NAME, where, whereValue);
  }
/**
* 查询全部表记录
* @return 返回查询的全部表记录
*/
public Cursor select()
  {
  SQLiteDatabase db=this.getReadableDatabase();
  Cursor cursor=db.query(TABLE_NAME, null, null, null, null, null,  " _id desc");
  return cursor;
  }
 
/**
* 更新数据
*/
public void update(int id,String Title)
  {
  SQLiteDatabase db=this.getWritableDatabase();
  String where=FIELD_ID+"=?";
  String[] whereValue={Integer.toString(id)};
  ContentValues cv=new ContentValues();
  cv.put(FIELD_TITLE, Title);
  db.update(TABLE_NAME, cv, where, whereValue);
  }
}
}
然后在主Activity中,通过db=new dbHelper(testDbActivity.this);调用Helper类完成表的创建,此时即可对返回db值进行操作,比如db.update(_id,  myEditText.getText().toString());实现文本框的更新等等。
/**
* 用listView来显示数据db
*/
myCursor=db.select();
        SimpleCursorAdapter adpater=new SimpleCursorAdapter(this
        		, R.layout.wuyue16, myCursor,
        		new String[]{dbHelper.FIELD_TITLE},
        		new int[]{R.id.listview});
        myListView.setAdapter(adpater);
然后就可以获取item的_id,实现对数据的操作。如下
myListView.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				SQLiteCursor sc=(SQLiteCursor)arg0.getSelectedItem();
				_id=sc.getInt(0);
				myEditText.setText(sc.getString(1));
			}
posted @ 2017-05-16 22:27  孙耀威  阅读(84)  评论(0编辑  收藏  举报