public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
//数据库生成
public void onCreate(SQLiteDatabase sqLiteDatabase) {
}
@Override
//数据库升级
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
DBOpenHelper helper=new DBOpenerHelper(this,"数据库名称.db",null,1);
SQLiteDatabase databaseWriter=helper.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("name","小张");
cv.put("sex","男");
databaseWriter.insert("user",null,cv);
databaseWriter.close();
Cursor c=dbRead.query("user",new String[]{"name","sex"},"name=?",new String[]{"小张" },null,null);
while(c.moveToNext()){
String name = c.getString(c.getColumnIndex("name")); //利用列的索引获取用户名
String sex=c.getString(c.getColumnIndex("sex"));
}
adapter=new SimpleCursonAdapter(this,layout,c,new String[]{"name","sex"},new int[]{R.id.tvName,R.id.tvSex});
setListAdapter(adapter);
sqliteDatabase.execSQL("create table user(_id integer primary key autoincrement,name text default null,sex text default null)");
//全局中定义
private
DBOpenHelper helper;private SQLiteDatabase databaseWriter,databaseRead;private Onclick btnAddListener=new OnClickListener(){
public void onClick(View v){
ContentValues cv = new ContentValues();
cv.put("name",et_name.getText().toString());cv.put("sex",et_sex.getText().toString());databaseWriter.insert("user",null,cv);refreshListView();}
};
btn_add.setOnClickListener(btnAddListener);
private void refreshListView(){ //两次调用该方法 类中调用一次。
Cursor c = databaseRead.query("user",null,null,null,null,null,null);
adapter.changeCursor(c);
}
getListView().setOnItemLongCilckListener(new OnItemLongCilckListener(
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(){
public void onClick(DialogInterface dialog,int which){
Cursor c = adapeter.getCursor();
c.moveToPosition(position);
int itemId=c.getInt(c.getColunmIndex("_id"):
databaseWriter.delete("user","_id=?",new String[]{itemId+""});
refreshListView();
}
}).show();//创建一个弹出对话框
return true; //是否执行长按
}
));
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by 59666 on 2016/10/20.
*/
public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table user(_id integer primary key autoincrement,name text default null,sex text default null)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private DBOpenHelper dbOpenHelper;
private SimpleCursorAdapter adapter;
private SQLiteDatabase sqliteWrite, sqliteRead;
private EditText et_name, et_sex;
private Button btn_add;
private ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbOpenHelper = new DBOpenHelper(this, "users", null, 1);
sqliteWrite = dbOpenHelper.getReadableDatabase();
sqliteRead = dbOpenHelper.getReadableDatabase();
list = (ListView) findViewById(R.id.list_item);
et_name = (EditText) findViewById(R.id.et_name);
et_sex = (EditText) findViewById(R.id.et_sex);
btn_add = (Button) findViewById(R.id.btn_add);
refreshListView();
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView> adapterView, View view, final int position, long id) {
new AlertDialog.Builder(MainActivity.this).setTitle("提示消息").setMessage("您确定要删除该条数据吗").setNegativeButton("取消", null).setPositiveButton("废话", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Cursor c = adapter.getCursor();
c.moveToPosition(position);
int itemId = c.getInt(c.getColumnIndex("_id"));
sqliteWrite.delete("user", "_id=?", new String[]{itemId + ""});
refreshListView();
}
}).show();
return true;
}
});
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ContentValues cv = new ContentValues();
cv.put("name", et_name.getText().toString().trim());
cv.put("sex", et_sex.getText().toString().trim());
sqliteWrite.insert("user", null, cv);
refreshListView();
}
});
}
private void refreshListView() {
Cursor c = sqliteRead.query("user", null, null, null, null, null, null);
adapter = new SimpleCursorAdapter(this, R.layout.user_layout, c, new String[]{"name", "sex"}, new int[]{R.id.tv_name, R.id.tv_sex});
list.setAdapter(adapter);
adapter.changeCursor(c);
}
}
xml version="1.0" encoding="utf-8"?>
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:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.user.MainActivity">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
android:id="@+id/et_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="用户名" />
android:id="@+id/et_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/tv_sex"
android:hint="性别" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:id="@+id/btn_add"
android:text="添加数据"/>
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="39dp"
android:layout_marginTop="17dp"
android:text="用户名" />
android:id="@+id/tv_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/tv_name"
android:layout_below="@+id/tv_name"
android:layout_marginTop="13dp"
android:text="性别" />