Trang 2 của 6 Đầu tiênĐầu tiên 1234 ... CuốiCuối
Kết quả 11 đến 20 của 59
  1. #11
    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] 

  2. #12
    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);
    }

    }
    }

  3. #13
    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 ^^

  4. #14
    Ngày tham gia
    Apr 2016
    Bài viết
    0
    bai viet hay

  5. #15
    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.

  6. #16
    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

  7. #17
    Ngày tham gia
    Apr 2014
    Bài viết
    1
    Trích dẫn Gửi bởi habaoanh
    /*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.
    hàm dựng là hàm khởi tạo đối tượng, là hàm cơ bản nhất trong lập trình java! là hàm đưa các đối số cần thiết (nếu có) ví dụ ở trên là đưa context vào!
    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!

  8. #18
    Ngày tham gia
    Apr 2016
    Bài viết
    0
    bài này hay lắm

  9. #19
    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??

  10. #20
    Ngày tham gia
    Apr 2015
    Bài viết
    0
    lớp con hay tách lớp đều đc e!

Trang 2 của 6 Đầu tiênĐầu tiên 1234 ... CuốiCuối

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •