SQLite和listview

首先创建一个类Sqlite继承自SQLiteOpenHelper,复写onCreate方法,在方法体中创建数据库的表格:
public class Sqlite extends SQLiteOpenHelper {
public Sqlite(Context context) {
super(context, "db", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table user(_id integer primary key autoincrement,name text,sex text)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}

主布局中最上面定义一个listview,下面定义两个textview和两个editview:


xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>

android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"/>
android:id="@+id/etName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sex"/>
android:id="@+id/etSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="确定"/>


SQLite和listview_第1张图片


接着,再新建一个布局文件,名为ueser_list_cell.xml:


android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/tvName" />

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/tvSex" />

注意:activity是继承自ListActivity,所以主布局有且仅能有一个listview,并且id的定义必须为
android:id="android:id/list"


public class MainActivity extends ListActivity {

private SimpleCursorAdapter simpleCursorAdapter;
private EditText etName,etSex;
private Sqlite db;
private SQLiteDatabase dbRead,dbWrite;
private ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

etName = (EditText)findViewById(R.id.etName);
etSex = (EditText)findViewById(R.id.etSex);
list = (ListView) findViewById(android.R.id.list);
// 继承自ListActivity,listview的id必须是android:id/list,所以这里找的id是android.R.id.list,
// 即系统的id
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues cv = new ContentValues();
cv.put("name",etName.getText().toString());
cv.put("sex",etSex.getText().toString());

dbWrite.insert("user",null,cv);
// 以上代码是把用户输入的名字和性别存储到数据库
refreshListView();
}
});
db = new Sqlite(this);

dbRead = db.getReadableDatabase();
dbWrite = db.getWritableDatabase();


simpleCursorAdapter = new SimpleCursorAdapter(this,R.layout.user_list_cell,null,new String[]{"name","sex"},new int[]{R.id.tvName,R.id.tvSex},0);
list.setAdapter(simpleCursorAdapter);

refreshListView();

getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) {

new AlertDialog.Builder(MainActivity.this)
.setTitle("提醒")
.setMessage("确定要删除吗?")
.setNegativeButton("取消",null)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Cursor c = simpleCursorAdapter.getCursor();
c.moveToPosition(position);

int itemId = c.getInt(c.getColumnIndex("_id"));
dbWrite.delete("user","_id=?",new String[]{itemId+""});
refreshListView();

}
}).create().show();


return true;
}
});
}
public void refreshListView(){
Cursor c = dbRead.query("user",null,null,null,null,null,null);
simpleCursorAdapter.changeCursor(c);
}
}

你可能感兴趣的:(SQLite和listview)