-
05-12-2013, 07:00 AM #11
Junior Member
- Ngày tham gia
- Apr 2015
- Bài viết
- 0
cái này là phần cơ bản của java, em hãy vào xem class mydatabase
trong class này có 1 hàm dựng (hàm khởi tạo đối tượng) với đối số truyền vào là 1 context , this ở đây chính là "mainactivity".
Mã nguồn PHP:[color=#000000]
[/color][color=#ff8000]/*hàm dựng, khởi tạo đối tượng*/ [/color][color=#007700]public [/color][color=#0000bb]mydatabase[/color][color=#007700]([/color][color=#0000bb]context c[/color][color=#007700]){ [/color][color=#0000bb]mydatabase[/color][color=#007700].[/color][color=#0000bb]context [/color][color=#007700]= [/color][color=#0000bb]c[/color][color=#007700]; }[/color]
-
05-17-2013, 07:00 AM #12
Junior Member
- Ngày tham gia
- Apr 2016
- Bài viết
- 0
moi nguoi oi xem cho mih bai nay voi ko hieu sao mih ko add dc du lieu vao bang
package com.example.an_demo_sqlite3;
import android.content.contentvalues;
import android.content.context;
import android.database.cursor;
import android.database.sqlexception;
import android.database.sqlite.sqlitedatabase;
import android.database.sqlite.sqlitedatabase.cursorfacto ry;
import android.database.sqlite.sqliteopenhelper;
public class mydatabase {
private static final string database_name ="database1";
private static final int database_version=1;
//
private static final string table_account="table1";
private static final string key_id="_id";
private static final string key_taikhoan="taikhoan";
private static final string key_pass="password";
private static final string creat_table="create table"+table_account+"("+key_id+"integer primary key autoincrement,"
+key_taikhoan+"text not null,"+key_pass+"text not null)";
//
private context context ;
private sqlitedatabase db;
private dbhelper dbh;
//
public mydatabase(context c ){
this.context=c;
}
public mydatabase open() throws sqlexception{
dbh= new dbhelper(context);
db=dbh.getwritabledatabase();
return this;
}
public void close(){
db.close();
}
public void adduser(taikhoan taikhoang){
contentvalues cv = new contentvalues();
cv.put(key_taikhoan, taikhoang.getuser());
cv.put(key_pass, taikhoang.getpassword());
db.insert(table_account, null, cv);
}
public string getalluser(){
string txt="";
cursor c =db.rawquery("select * from"+table_account, null);
if(c.movetofirst()){
do{
txt+="id- "+c.getstring(0)+" taikhoan- "+c.getstring(1)+" pass- "+c.getstring(2)+"
";
}while(c.movetonext());
}
c.close();
return txt;
}
private static class dbhelper extends sqliteopenhelper{
public dbhelper(context contex
) {
super(contex, database_name, null, database_version);
// todo auto-generated constructor stub
}
@override
public void oncreate(sqlitedatabase ar) {
// todo auto-generated method stub
ar.execsql(creat_table);
}
@override
public void onupgrade(sqlitedatabase ar, int oldversion, int newversion) {
// todo auto-generated method stub
ar.execsql("drop table if exists"+table_account);
oncreate(ar);
}
}
}
-
05-18-2013, 07:00 AM #13
Junior Member
- Ngày tham gia
- Nov 2014
- Đang ở
- Hà Nội
- Bài viết
- 0
đã fix rồi nhé, câu lệnh khởi tạo bị sai ^^
-
-
05-19-2013, 07:00 AM #15
Junior Member
- Ngày tham gia
- May 2014
- Bài viết
- 0
/*hàm dựng, khởi tạo đối tượng*/
public mydatabase(context c){
mydatabase.context = c;
}
các bác cho hỏi: ý nghĩa của hàm này là gi, khởi tạo đối tượng là khởi tạo đối tượng gì, thông cảm e chỉ là tay ngang thôi.
-
05-19-2013, 07:00 AM #16
Junior Member
- Ngày tham gia
- Feb 2015
- Bài viết
- 0
doi tuong la mydatabase thoi b.
trong mydatabase co nh gi thi coi nhu minh tao moi,
vd
class student{
// properties of each student
string mname;
int mid;
// constructor to create an object
public student(string name, int id){
this.mname=name;
this.mid=id;
}
}
do ban hieu constructor nay thi cung se hieu dan constructor mydatabase
-
05-19-2013, 07:00 AM #17
Junior Member
- Ngày tham gia
- Apr 2014
- Bài viết
- 1
Gửi bởi habaoanh
nếu bạn chưa hiểu thì có thể search với google "vi du ve khoi tao doi tuong trong java" nhé, mình có tìm thử và thấy nhiều bài hướng dẫn lắm!
-
-
05-25-2013, 07:00 AM #19
Junior Member
- Ngày tham gia
- Mar 2015
- Bài viết
- 0
tại sao không tách riêng class openhelper ra hả anh??
-
05-25-2013, 07:00 AM #20
Junior Member
- Ngày tham gia
- Apr 2015
- Bài viết
- 0
lớp con hay tách lớp đều đc e!
Phương pháp đả hường vùng đặc cọ công nghệ Laser – S chẳng đang là chuyện xa kì cọ đối cùng nhiều chị em phụ nữ bây giờ. nhờ vả tác dụng giảm thâm tuyệt trần, giúp “vùng đặc” mau chóng trở nên hồng...
Công nghệ Laser S làm hồng vùng kín: Giải pháp an toàn và hiệu quả