Android数据库相关代码解读之 SQLLite
2010-05-19 16:17:00 来源:WEB开发网/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.TEditname = (EditText) this.findViewById
(R.id.widgeteditname);
this.TEditsex = (EditText) this.findViewById
(R.id.widgeteditsex);
this.TEditages = (EditText) this.findViewById
(R.id.widgeteditages);
TEditmerge = (EditText) this.findViewById
(R.id.widgeteditmerge);
this.TSavebutton = (Button) this.findViewById
(R.id.widgetSavebutton);
TViewbutton = (Button) this.findViewById
(R.id.widgetViewbutton);
this.db = new DBHelper(this);
this.TSavebutton.setOnClickListener(this);
this.TViewbutton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// this.TEditages.setText(this.TEditname.getText().
toString()+this.TEditsex.getText().toString());
if (v.getId() == R.id.widgetSavebutton) {
try {
this.db.open(this);
this.db.save(this.TEditname.getText().toString(), this.TEditsex
.getText().toString(), Integer.valueOf(this.TEditages
.getText().toString()));
} catch (Exception e) {
Log.v("save data", "save data fail");
} finally {
this.db.close();
}
} else if (v.getId() == R.id.widgetViewbutton && null != db) {
this.db.open(this);
// 浏览所有数据
Cursor cur = db.loadAll();
StringBuffer sf = new StringBuffer();
cur.moveToFirst();
while (!cur.isAfterLast()) {
sf.append(cur.getInt(0)).append(" : ").append(cur.getString(1))
.append(" : ").append(cur.getString(2)).append(" : ")
.append(cur.getInt(3)).append(" ");
cur.moveToNext();
}
db.close();
this.TEditmerge.setText(sf.toString());
}
}
}
赞助商链接