学了SQLite之后,还是有点晕晕乎乎的,靠个例子练练手可能就会明白了,当然了分享出来,还有更新的新手呢!!
先上图:
上代码:
public class InsertActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert);
Button commit=(Button) findViewById(R.id.insert_commit);
commit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText et1=(EditText)findViewById(R.id.insert_name);
EditText et2=(EditText)findViewById(R.id.insert_age);
try{
Person person=new Person(et1.getText().toString(),
Integer.parseInt(et2.getText().toString()));
insert(person);
}catch(Exception e){
Toast.makeText(getApplicationContext(), "姓名或年龄不能为空!", 1).show();
}
setResult(RESULT_OK, null);
finish(); //不能少
}
});
Button cancle=(Button) findViewById(R.id.insert_cancle);
cancle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
public void insert(Person person){
DBOpenHelper helper = new DBOpenHelper(getApplicationContext(), "test.db", null,1);
SQLiteDatabase db=helper.getWritableDatabase();
db.execSQL("INSERT INTO person VALUES (NULL, ?, ?)",
new Object[ ] { person.name, person.age } );
db.close();
Toast.makeText(getApplicationContext(), "记录添加成功", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.insert, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
public class MainActivity extends Activity implements OnClickListener{
private static int DB_VERSION=1;
private Button button1=null,button2=null;//,button3=null,button4=null,button5=null,button6=null;
private TextView textview=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findview();
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
public void findview(){
button1=(Button) findViewById(R.id.button1);
button2=(Button) findViewById(R.id.button2);
textview=(TextView) findViewById(R.id.textView1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
//创建辅助类对象
DBOpenHelper helper = new DBOpenHelper(getApplicationContext(), "test.db", null,1);
//调用getWritableDatabase()或getReadableDatabase()才会真正创建或打开
SQLiteDatabase db=helper.getWritableDatabase();
db.close(); //操作完成后关闭数据库连接
break;
case R.id.button2:
Intent intent1=new Intent(MainActivity.this,ShowActivity.class);
startActivity(intent1);
break;
default:
break;
}
}
}
public class ShowActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
DBOpenHelper helper = new DBOpenHelper(getApplicationContext(), "test.db", null,1);
SQLiteDatabase db=helper.getWritableDatabase();
Cursor cursor = db.rawQuery( "SELECT id as _id, name,age FROM person",null);
String[] from = { "_id", "name", "age" };
int[] to = { R.id.txtID, R.id.txtName, R.id.txtAge };
SimpleCursorAdapter adapter = new SimpleCursorAdapter( this, R.layout.listview, cursor, from, to);
//cursor.close();不能使用cursor.close(),否则SimpleCursorAdapter将不能从中读取数据显
TextView tv=(TextView)findViewById(R.id.show_tv); //简单用TextView显示数据
tv.setText("查询到"+cursor.getCount()+"条记录");
ListView li=(ListView)findViewById(R.id.listView1);
li.setAdapter(adapter);
Button button=(Button) findViewById(R.id.add);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(ShowActivity.this,InsertActivity.class);
startActivityForResult(intent, 100);
}
});
registerForContextMenu(li);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==100){
if(resultCode==RESULT_OK){
onCreate(null);
}
}
if(requestCode==200)
if(resultCode==RESULT_OK){
onCreate(null);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.show, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("操作");
getMenuInflater().inflate(R.menu.show, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch ( item.getItemId() ) {
case R.id.item1:
del(item);
return true;
case R.id.item2:
change(item);
return true;
default:
return false;
}
}
private void change(MenuItem item) {
final AdapterView.AdapterContextMenuInfo info2= (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
Intent intent =new Intent(ShowActivity.this, UpdateActivity.class);
Bundle bundle=new Bundle();
bundle.putString("id", String.valueOf(info2.id));
bundle.putString("username",((TextView)info2.targetView.findViewById(R.id.txtName)).getText().toString());
bundle.putString("age",((TextView)info2.targetView.findViewById(R.id.txtAge)).getText().toString());
intent.putExtras(bundle);
startActivityForResult(intent, 200);
}
private void del(MenuItem item) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
if(info.id>0){
new AlertDialog.Builder(this).setTitle("删除" + info.id)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
DBOpenHelper helper = new DBOpenHelper(getApplicationContext(), "test.db", null,1);
SQLiteDatabase db=helper.getWritableDatabase();
db.execSQL( "Delete from person where id=?", new Object[ ]{ info.id } );
db.close();
Toast.makeText(getApplicationContext(), "记录删除成功", Toast.LENGTH_SHORT).show();
onCreate(null); //重新加载一次Activity,刷新
}
})
.setNegativeButton("取消", null)
.show();
}
}
}