使用listview查看:https://www.jianshu.com/p/392a7c090cae
DbAdapter
package com.example.sqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DBAdapter {
private static final StringDB_NAME ="student.db";
private static final StringDB_TABLE ="stuInfo";
private static int DB_VERSION =1;
private static final StringKEY_NAME ="name";
private static final StringKEY_SNO ="sno";
private static final StringKEY_CNO ="cno";
private static final StringKEY_ID ="id";
private static SQLiteDatabasedb;
private Contextcontext;
private DBopenhelperdBopenhelper;
public DBAdapter(Context context)
{
this.context = context;
}
public long insert(stuInfo stu)
{
ContentValues newValues =new ContentValues();
newValues.put(KEY_SNO, stu.GetSno());
newValues.put(KEY_CNO, stu.GetCno());
newValues.put(KEY_NAME, stu.GetName());
return db.insert(DB_TABLE, null, newValues);
}
public long deleteAllData()
{
return db.delete(DB_TABLE, null, null);
}
public long deleteOneData(long id)
{
return db.delete(DB_TABLE, KEY_ID +"=" + id, null);
}
public stuInfo[]ConvertTostuInfo(Cursor cursor)
{
int resCount = cursor.getCount();
if(resCount==0 || !cursor.moveToFirst()){
return null;
}
stuInfo[] stu =new stuInfo[resCount];
for(int i=0; i
stu[i] =new stuInfo(cursor.getString(cursor.getColumnIndex(KEY_ID)) +" " + cursor.getString(cursor.getColumnIndex(KEY_NAME)), cursor.getString(cursor.getColumnIndex(KEY_CNO)),cursor.getString(cursor.getColumnIndex(KEY_SNO)));
cursor.moveToNext();
}
return stu;
}
public stuInfo[]getAllData()
{
Cursor result =db.query(DB_TABLE, new String[] {KEY_ID, KEY_SNO, KEY_CNO, KEY_NAME}, null, null, null, null, null);
return ConvertTostuInfo(result);
}
public void open()throws SQLiteException{
dBopenhelper =new DBopenhelper(context, DB_NAME, null, DB_VERSION);
try{
db =dBopenhelper.getWritableDatabase();
}catch (SQLiteException e){
db =dBopenhelper.getReadableDatabase();
}
}
public void close()
{
if(db!=null){
db.close();
db=null;
}
}
private static class DBopenhelperextends SQLiteOpenHelper{
public DBopenhelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
super(context, name, factory, version);
}
private final StringDB_Create="create table " +DB_TABLE +"(" +KEY_ID +" integer primary key autoincrement,"
+KEY_SNO +" text not null," +KEY_NAME +" text not null, " +KEY_CNO +" text not null);";
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DB_Create);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldViersion, int _newVersion) {
_db.execSQL("DROP TABLE IF EXISTS " +DB_TABLE);
onCreate(_db);
}
}
}
sqlite
package com.example.sqlite;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class sqliteextends AppCompatActivityimplements Button.OnClickListener{
ListViewlistView;
Listdata =new ArrayList<>();
Buttonrefresh, insert, clear;
TextViewname, sno, cno;
infoAdapteradapter;
int item_id;
DBAdapterdbAdapter =new DBAdapter(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sqlite);
refresh = (Button) findViewById(R.id.refresh);
insert = (Button) findViewById(R.id.insert);
clear = (Button) findViewById(R.id.clear);
refresh.setOnClickListener(this);
insert.setOnClickListener(this);
clear.setOnClickListener(this);
name = (TextView)findViewById(R.id.name);
sno = (TextView) findViewById(R.id.sno);
cno = (TextView) findViewById(R.id.cno);
adapter =new infoAdapter(this, data);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
registerForContextMenu(listView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
item_id = info.position;
menu.add(0, Menu.FIRST, 0, "删除");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
dbAdapter.open();
dbAdapter.deleteOneData((Integer.parseInt(data.get(item_id).GetName().split(" ")[0])));
dbAdapter.close();
data.remove(item_id);
adapter.notifyDataSetChanged();
return true;
}
public void Insert()
{
stuInfo stu =new stuInfo(name.getText().toString(),cno.getText().toString(), sno.getText().toString());
dbAdapter.open();
dbAdapter.insert(stu);
getData();
dbAdapter.close();
}
public void getData()
{
dbAdapter.open();
stuInfo stus[] =dbAdapter.getAllData();
if(stus ==null){
data.clear();
adapter.notifyDataSetChanged();
return;
}
dbAdapter.close();
data.clear();
for(int i=0; i
{
data.add(stus[i]);
}
adapter.notifyDataSetChanged();
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.refresh:
getData();
break;
case R.id.insert:
Insert();
break;
case R.id.clear:
dbAdapter.open();
dbAdapter.deleteAllData();
dbAdapter.close();
getData();
break;
}
}
}