private void init()(
mEcName =(EditText)findViewByld(R.id.et name); mEtPhone #(EditText)findViewById(R.id.et phone); mTvShow=(TextView)findViewById(R.id.tv show); mBtnAdd=(Button) findViewById(R.id.btn add);
mBtnQuery=(Button) findViewById(R.id.btn query); mBtnUpdate *(Button) findViewById(R.id.btn update); mBtnDelete=(Button)findViewById(R.id.btn delete); mBtnAdd.setOnClickListener(this); mBtnQuery.setOnClickListener(this); mBtnUpdate.setOnClickListener(this); mBtnDelete.setOnClickListener(this);
@Override
public void onClick(View v)(
String name; String phone;
SQLiteDatabase db;
ContentValues values; switch(v.getId())(
case R.id.btn add://添加数据
name:=mEtName.getText()toString(); phone =mEtPhone.getText().toString();
dbmyHelper.getWritableDatabase();//获取可读写SQLiteDatabase 对象 values =new ContentValues(); //创建Contentvalues对象
values.put(“name”,name); //将数据添加到ContentValues对象
values.put(“phone”,phone);
db.insert(“information”, null, values);
Toast.makeText(this,“信息已添加”,Toast.LENGTH_SHORT).show(); db.close(); break;
case R.id.btn_query://查询数据
db=myHelper.getReadableDatabase();
Cursor cursor =db.query(“information”, null, null, null, null
null,null);
if (cursor.getCount() ==0){
mTvShow.setText("");
Toast.makeText(this,“没有数据”,Toast.LENGTH SHORT).show();} else {
cursor.moveToFirst();
mTvShow.setText(“Name :”+cursor.getString(1)+
;Tel:"+ cursor.getString(2));
while (cursor.moveToNext())【
mTvShow.append("\n"+“Name:”+cursor.getString(l)+
" :Tel :"+cursor.getString(2));
cursor.close(); db.close(); break;
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交