Sprint 冲刺第三阶段第3-5天 数据库代码
数据库代码:
package com.example.brdemo; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.net.Uri; import android.os.Bundle; import android.support.v4.widget.CursorAdapter; import android.support.v4.widget.SimpleCursorAdapter; import android.view.Menu; import android.view.MenuInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private Button btn1,btn2,btn3; private EditText edname,edsecret; Intent iguangs, iches; SQLiteDatabase db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button) findViewById(R.id.b1);// 注册 btn2 = (Button) findViewById(R.id.bt3);// 测试 btn3 = (Button) findViewById(R.id.b2);// 登录 edname = (EditText) findViewById(R.id.editname); edsecret = (EditText) findViewById(R.id.editsecret); db=SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/mydb.db3", null); // 注册 btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { String title,content; title=edname.getText().toString(); content=edsecret.getText().toString(); try { insertData(db, title, content); Cursor cursor=db.rawQuery("select * from myTable", null); inflateList(cursor); } catch (SQLiteException e) { // TODO: handle exception db.execSQL("create table myTable(_id integer" +"primary key autonicrement," +"title varchar(50)," + "content varchar(255))"); insertData(db, title, content); Cursor cursor=db.rawQuery("select * from myTable", null); inflateList(cursor); } Toast.makeText(MainActivity.this, "用户"+edname.getText() +"注册成功", Toast.LENGTH_LONG).show(); iguangs = new Intent(); Bundle bundle = new Bundle(); iguangs.setClass(MainActivity.this, H.class); bundle.putString("userName", edname.getText().toString()); bundle.putString("phoneNum", edsecret.getText().toString()); iguangs.putExtras(bundle); // 启动Activity startActivity(iguangs); } private void inflateList(Cursor cursor) { // TODO Auto-generated method stub } private void insertData(SQLiteDatabase db, String title, String content) { // TODO Auto-generated method stub } }); // 测试 btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Uri uri = Uri.parse("http://open.taobao.com"); iches = new Intent(Intent.ACTION_VIEW, uri); startActivity(iches); } }); // 登录 btn3.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { iguangs = new Intent(); Bundle bundle = new Bundle(); iguangs.setClass(MainActivity.this, H.class); bundle.putString("userName", edname.getText().toString()); bundle.putString("phoneNum", edsecret.getText().toString()); Toast.makeText(MainActivity.this, "用户"+edname.getText() +"登录成功", Toast.LENGTH_LONG).show(); iguangs.putExtras(bundle); // 启动Activity startActivity(iguangs); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub MenuInflater inflater = new MenuInflater(this); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } private void insertData(SQLiteDatabase db,String title,String content) { db.execSQL("insert into myTable values(null,?,?)",new String[]{title,content}); } private void inflateList(Cursor cursor) { SimpleCursorAdapter simpleCursorAdapter=new SimpleCursorAdapter(MainActivity.this, R.layout.activity_main, cursor, new String[]{"title","content"}, new int[]{R.id.editname,R.id.editsecret}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); if (db!=null &&db.isOpen()) { db.close(); } } }