日期:2014-05-20 浏览次数:20685 次
public class SQLITEPage extends RelativeLayout {
Activity activity;
Context context;
Context othercontext;
TextView showresult;
Button insertbtn;
Button updatebtn;
Button selectbtn;
Button deletebtn;
public CopyOfShowPage(Context context) {
super(context);
this.context = context;
this.activity =(Activity)activity;
init();
}
private void init() {
// TODO Auto-generated method stub
MyListen myListen = new MyListen();
showresult=new TextView(context);
showresult.setBackgroundColor(Color.WHITE);
showresult.setId(99);
showresult.setText("空");
LayoutParams lpshowresult = new LayoutParams(-1, -2);
lpshowresult.addRule(RelativeLayout.ALIGN_PARENT_TOP);
insertbtn = new Button(context);
insertbtn.setText("插入");
insertbtn.setId(100);
insertbtn.setBackgroundColor(Color.YELLOW);
LayoutParams lpinsertbtn = new LayoutParams(-1, -2);
lpinsertbtn.addRule(RelativeLayout.BELOW, 99);
insertbtn.setOnClickListener(myListen);
updatebtn = new Button(context);
updatebtn.setText("更新");
updatebtn.setId(101);
updatebtn.setBackgroundColor(Color.YELLOW);
LayoutParams lpupdatebtn = new LayoutParams(-1, -2);
lpupdatebtn.addRule(RelativeLayout.BELOW, 100);
updatebtn.setOnClickListener(myListen);
selectbtn = new Button(context);
selectbtn.setText("查询");
selectbtn.setId(102);
selectbtn.setBackgroundColor(Color.YELLOW);
LayoutParams lpselectbtn = new LayoutParams(-1, -2);
lpselectbtn.addRule(RelativeLayout.BELOW, 101);
selectbtn.setOnClickListener(myListen);
deletebtn = new Button(context);
deletebtn.setText("删除");
deletebtn.setId(103);
deletebtn.setBackgroundColor(Color.YELLOW);
LayoutParams lpdeletebtn = new LayoutParams(-1, -2);
lpdeletebtn.addRule(RelativeLayout.BELOW, 102);
deletebtn.setOnClickListener(myListen);
addView(showresult, lpshowresult);
addView(insertbtn, lpinsertbtn);
addView(updatebtn, lpupdatebtn);
addView(selectbtn, lpselectbtn);
addView(deletebtn, lpdeletebtn);
}
// 得到数据库
public SQLiteDatabase getDb() {
DBHelper dbhelper = new DBHelper(othercontext);
SQLiteDatabase db = dbhelper.getWritableDatabase();
return db;
}
// 增加
public void insert(SQLiteDatabase db, List<User> userlist) {
for (User user : userlist) {
ContentValues values = new ContentValues();
values.put("_id", user.get_id());
values.put("name", user.getName());
values.put("info", user.getInfo());
values.put("age", user.getAge());
db.insert(DBHelper.TABLE_NAME, null, values);
}
}
// 删除
public void delete(SQLiteDatabase db, List<Integer> idlist) {
for (Integer i : idlist) {
db.delete(DBHelper.TABLE_NAME, "id = ?", new String[] { i.toString() });
}
}
// 修改
public void update(SQLiteDatabase db, List<User> userlist) {
for (User user : userlist) {
ContentValues values = new ContentValues();
values.put("name", user.getName());
values.put("info", user.getInfo());
values.put("age", user.getAge());
db.update(DBHelper.TABLE_NAME, values, "_id=",
new String[] { Integer.toString(user.get_id()) });
}
}
// 查询
public List<User> queryAll(SQLiteDatabase db) {
ArrayList<User> list = new ArrayList<User>();
Cursor c = db.rawQuery("SELECT * FROM "+DBHelper.TABLE_NAME, null);
while (c.moveToNext()) {
for(int i=0;i<c.getColumnCount();i++){
System.out.print(c.getColumnName(i).toString()+":"+c.getString(i)+",");
}
System.out.println();
}
c.close();
return list;
}
class MyListen implements OnClickListener {
@Override
public void onClick(View v) {