Sqlite trong android (part 2) – multiple tables

 Download demo

Như đã nói với các bạn ở bài trước bài này mình sẽ hướng dẫn các bạn làm việc với nhiều bảng CSDL trong SQLite.

Do ví dụ này mục đích được viết để hướng dẫn các bạn làm việc với nhiều bảng CSDL và cách lấy dữ liệu trong các bảng này nên không chú trọng đến tính ràng buộc dữ liệu các bạn muốn làm 1 app tốt về SQLite thì các bạn cần phải thiết kế CSDL các bảng theo chuẩn “3NF” và hiểu về cấu trúc DB.

Trong ví dụ này thể hiện cách xây dựng 3 bảng CSDL và các hàm truy vấn và sửa đổi các bảng dữ liệu

Để làm ví dụ này các bạn các bạn cần thực hiện theo lần lượt các bước sau

Bước 1: mở file Strings.xml

<?xml version="1.0" encoding="utf-8"?></pre>

<resources>



<string name="app_name">SQLiteTuto</string>

 <string name="action_settings">Settings</string>

 <string name="hello_world">Hello world!</string>

 <string name="student_name">Vu Manh Hung</string>

 <string name="student_add">Hanoi</string>

 <string name="student_id">1</string>

 <string name="course_name">Co So Du Lieu</string>

 <string name="course_id">1</string>

 <string name="course_tc">5</string>

 <string name="tv_name">Name :</string>

 <string name="tv_id">ID :</string>

 <string name="tv_add">Address :</string>

 <string name="tv_tc">Tin Chi :</string>

 <string name="tv_student">Student</string>

 <string name="tv_course">Course</string>



</resources>

Bước 2: mở file activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"</pre>

xmlns:tools="http://schemas.android.com/tools"

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 android:paddingBottom="@dimen/activity_vertical_margin"

 android:paddingLeft="@dimen/activity_horizontal_margin"

 android:paddingRight="@dimen/activity_horizontal_margin"

 android:paddingTop="@dimen/activity_vertical_margin"

 tools:context=".MainActivity" >



<ListView

 android:id="@+id/lvStudents"

 android:layout_width="fill_parent"

 android:layout_height="match_parent" >

 </ListView>



</RelativeLayout>

Bước 3: tạo file layout_tiem.xml (mình muốn hiển thị dữ liệu select lên được ra listview với mỗi item có giao diện như  file này)

<?xml version="1.0" encoding="utf-8"?></pre>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 android:orientation="horizontal"

 android:padding="5dp" >



<LinearLayout

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:orientation="vertical" >



<TextView

 android:id="@+id/tvStudent"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:layout_gravity="center_horizontal"

 android:text="@string/tv_student" />



<LinearLayout

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:orientation="horizontal" >



<TextView

 android:id="@+id/tvName"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="@string/tv_name" />



<TextView

 android:id="@+id/tvStudentName"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="@string/student_name" />

 </LinearLayout>



<LinearLayout

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:orientation="horizontal" >



<TextView

 android:id="@+id/tvId"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="@string/tv_id" />



<TextView

 android:id="@+id/tvStudentId"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="@string/student_id" />

 </LinearLayout>



<LinearLayout

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:orientation="horizontal" >



<TextView

 android:id="@+id/tvAdd"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="@string/tv_add" />



<TextView

 android:id="@+id/tvStudentAdd"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="@string/student_add" />

 </LinearLayout>

 </LinearLayout>



<LinearLayout

 android:layout_width="match_parent"

 android:layout_height="wrap_content"

 android:orientation="vertical" >



<TextView

 android:id="@+id/tvCourse"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="@string/tv_course"

 android:layout_gravity="center_horizontal" />



<LinearLayout

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:layout_gravity="right"

 android:orientation="horizontal" >



<TextView

 android:id="@+id/tvName2"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="@string/tv_name" />



<TextView

 android:id="@+id/tvCourseName"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="@string/course_name" />

 </LinearLayout>



<LinearLayout

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:layout_gravity="right"

 android:orientation="horizontal" >



<TextView

 android:id="@+id/tvId2"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="@string/tv_id" />



<TextView

 android:id="@+id/tvCourseId"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="@string/course_id" />

 </LinearLayout>



<LinearLayout

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:layout_gravity="right"

 android:orientation="horizontal" >



<TextView

 android:id="@+id/tvTc"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="@string/tv_tc" />



<TextView

 android:id="@+id/tvCourseTc"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="@string/course_tc" />

 </LinearLayout>

 </LinearLayout>



</LinearLayout>

Bước 4: tạo class LogUtil.java trong packet  com.hungvm.sqlitetdemo3.logutil

package com.hungvm.sqlitetdemo3.logutil;



import android.util.Log;



public class LogUtil {

 public static final boolean WRITELOG = true;



 public static void LogD(String tag, String msg) {

 if(WRITELOG) {

 Log.d(tag, msg);

 }

 }



 public static void LogE(String tag, String msg) {

 if(WRITELOG) {

 Log.e(tag, msg);

 }

 }



 public static void LogI(String tag, String msg) {

 if(WRITELOG) {

 Log.i(tag, msg);

 }

 }



 public static void LogW(String tag, String msg) {

 if(WRITELOG) {

 Log.w(tag, msg);

 }

 }

}

Bước 5: tạo class Course.java trong packet com.hungvm.sqlitedemo3.model

package com.hungvm.sqlitedemo3.model;



public class Course {

 int _id, tc;

 String name;



public Course() {

 super();

 }



public Course(int _id, int tc, String name) {

 super();

 this._id = _id;

 this.tc = tc;

 this.name = name;

 }



 public Course(int tc, String name) {

 super();

 this.tc = tc;

 this.name = name;

 }



public int get_id() {

 return _id;

 }



public void set_id(int _id) {

 this._id = _id;

 }



public int getTc() {

 return tc;

 }



public void setTc(int tc) {

 this.tc = tc;

 }



public String getName() {

 return name;

 }



public void setName(String name) {

 this.name = name;

 }



}

Bước 6: tạo class Student.java trong packet com.hungvm.sqlitedemo3.model

package com.hungvm.sqlitedemo3.model;



public class Student {

 int _id;

 String name, address;



public Student() {

 super();

 }



public Student(int _id, String name, String address) {

 super();

 this._id = _id;

 this.name = name;

 this.address = address;

 }



 public Student(String name, String address) {

 super();

 this.name = name;

 this.address = address;

 }



public int get_id() {

 return _id;

 }



public void set_id(int _id) {

 this._id = _id;

 }



public String getName() {

 return name;

 }



public void setName(String name) {

 this.name = name;

 }



public String getAddress() {

 return address;

 }



public void setAddress(String address) {

 this.address = address;

 }



}

Bước 7: tạo class Take.java trong packet com.hungvm.sqlitedemo3.model

package com.hungvm.sqlitedemo3.model;

public class Take {

 int _id;

 Student student;

 Course course;



 public Take() {



 }



public Take(int _id, Student student, Course course) {

 super();

 this._id = _id;

 this.student = student;

 this.course = course;

 }



public Take(Student student, Course course) {

 super();

 this.student = student;

 this.course = course;

 }



public int get_id() {

 return _id;

 }



public void set_id(int _id) {

 this._id = _id;

 }



public Student getStudent() {

 return student;

 }



public void setStudent(Student student) {

 this.student = student;

 }



public Course getCourse() {

 return course;

 }



public void setCourse(Course course) {

 this.course = course;

 }



}

Bước 8 tạo class DatabaseHelper trong packet com.hungvm.sqlitedemo3.dbhelper

package com.hungvm.sqlitedemo3.dbhelper;



import java.util.ArrayList;



import com.hungvm.sqlitedemo3.model.Course;

import com.hungvm.sqlitedemo3.model.Student;

import com.hungvm.sqlitedemo3.model.Take;

import com.hungvm.sqlitetdemo3.logutil.LogUtil;



import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;



public class DatabaseHelper extends SQLiteOpenHelper {



 // TAG cho cac log ve phan nay

 private static final String LOG = "DatabaseHelper";



 // ten CSDL

 private static final String DB_NAME = "MyDataBase.db";



 // phien ban cua CSDL

 private static final int DB_VERSION = 1;



 // ten cac bang trong CSDL

 private static final String TABLE_STUDENT = "students";

 private static final String TABLE_COURSE = "course";

 private static final String TABLE_TAKE = "takes";



 // ten cac cot chung

 private static final String KEY_ID = "_id";

 private static final String KEY_NAME = "name";



 // ten cac cot cua bang students

 private static final String KEY_ADDRESS = "address";



 // ten cac cot cua bang courses

 private static final String KEY_TC = "tc";



 // ten cac cot cua bang takes

 private static final String KEY_STUDENT_ID = "student_id";

 private static final String KEY_COURSE_ID = "course_id";



 // cau lenh SQL tao bang students

 private static final String CREATE_TABLE_STUDENT = "CREATE TABLE " + TABLE_STUDENT + " ("

 + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME + " TEXT, " + KEY_ADDRESS + " TEXT)";



 // cau lenh SQL tao bang courses

 private static final String CREATE_TABLE_COURSE = "CREATE TABLE " + TABLE_COURSE + " ("

 + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME + " TEXT, " + KEY_TC + " INTEGER)";



 // cau lenh SQL tao bang takes

 private static final String CREATE_TABLE_TAKE = "CREATE TABLE " + TABLE_TAKE + " ("

 + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_STUDENT_ID + " INTEGER, " + KEY_COURSE_ID + " INTEGER)";



 // khoi tao CSDL

 public DatabaseHelper(Context context) {

 super(context, DB_NAME, null, DB_VERSION);

 }



 // tao CSDL voi 3 bang student, course, take

 @Override

 public void onCreate(SQLiteDatabase db) {

 db.execSQL(CREATE_TABLE_STUDENT);

 db.execSQL(CREATE_TABLE_COURSE);

 db.execSQL(CREATE_TABLE_TAKE);

 }



 // update CSDL

 @Override

 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

 db.execSQL("DROP TABLE IF EXISTS " + TABLE_COURSE);

 db.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDENT);

 db.execSQL("DROP TABLE IF EXISTS " + TABLE_TAKE);

 onCreate(db);

 }



// them 1 sudent vao bang students

 public void insertStudent(Student student) {

 // cap quyen ghi CSDL cho bien database

 SQLiteDatabase database = this.getWritableDatabase();



 // dat cac gia tri cua student can them cho bien values

 ContentValues values = new ContentValues();

 values.put(KEY_NAME, student.getName());

 values.put(KEY_ADDRESS, student.getAddress());



 // them vao CSDL

 database.insert(TABLE_STUDENT, null, values);

 }



 // lay thong tin 1 student ra tu CSDL

 public Student getStudent(int id) {

 // cap quyen doc CSDL cho bien database

 SQLiteDatabase database = this.getReadableDatabase();



 // gan cau lenh SQL vao bien selectQuerry

 String selectQuery = "SELECT * FROM " + TABLE_STUDENT + " WHERE " + KEY_ID + " = " + id;



 // Log ra selectQuerry

 LogUtil.LogD(LOG, selectQuery);



 // doi tuong luu cac hang cua bang truy van

 Cursor c = database.rawQuery(selectQuery, null);



 // chuyen con tro den dong dau tien neu du lieu tra ve tu CSDL khong phai null

 if(c!= null) {

 c.moveToFirst();

 }



 // dong goi thong tin vao 1 doi tuong student

 Student student = new Student();

 student.set_id(c.getInt(c.getColumnIndex(KEY_ID)));

 student.setName(c.getString(c.getColumnIndex(KEY_NAME)));

 student.setAddress(c.getString(c.getColumnIndex(KEY_ADDRESS)));



 // tra ve 1 student

 return student;

 }



 // lay thong tin tat ca student ra tu CSDL

 public ArrayList<Student> getAllStudent() {

 ArrayList<Student> arrStudent = new ArrayList<Student>();



 SQLiteDatabase database = this.getReadableDatabase();

 String selectQuerry = "SELECT * FROM " + TABLE_STUDENT;



 LogUtil.LogD(LOG,selectQuerry);



 Cursor c = database.rawQuery(selectQuerry, null);



 if(c!=null) {

 c.moveToFirst();



 do{

 // dong goi thong tin vao 1 doi tuong student

 Student student = new Student();



 student.set_id(c.getInt(c.getColumnIndex(KEY_ID)));

 student.setName(c.getString(c.getColumnIndex(KEY_NAME)));

 student.setAddress(c.getString(c.getColumnIndex(KEY_ADDRESS)));



 arrStudent.add(student);

 } while(c.moveToNext()); // chuyen toi dong tiep theo

 }



 // tra ve danh sach cac student

 return arrStudent;

 }



 // sua thong tin 1 student co ID = _id

 public void updateStudent(Student student, int _id) {

 // cap quyen ghi cho bien database

 SQLiteDatabase database = this.getWritableDatabase();



 ContentValues values = new ContentValues();

 values.put(KEY_NAME, student.getName());

 values.put(KEY_ADDRESS, student.getAddress());



 // sua student co ID = _id theo cac thong tin trong bien values

 database.update(TABLE_STUDENT, values, KEY_ID + " = " + _id, null);

 }



 // xoa student co ID = _id

 public void deleteStudent(int _id) {

 SQLiteDatabase database = this.getWritableDatabase();



 database.delete(TABLE_STUDENT, KEY_ID + " = " + _id, null);

 }



 // them 1 course vao CSDL

 public void insertCourse(Course course) {

 SQLiteDatabase database = this.getWritableDatabase();



 ContentValues values = new ContentValues();

 values.put(KEY_NAME, course.getName());

 values.put(KEY_TC, course.getTc());



 database.insert(TABLE_COURSE, null, values);

 }



 // lay course co ID = _id ra tu CSDL

 public Course getCourse(int _id) {

 SQLiteDatabase database = this.getReadableDatabase();



 String selectQuerry = "SELECT * FROM " + TABLE_COURSE + " WHERE " + KEY_ID + " = " + _id;



 LogUtil.LogD(LOG, selectQuerry);



 Cursor c = database.rawQuery(selectQuerry, null);



 if(c!=null) {

 c.moveToFirst();

 }



 // dong goi du lieu vao doi tuong course

 Course course = new Course();

 course.set_id(c.getInt(c.getColumnIndex(KEY_ID)));

 course.setName(c.getString(c.getColumnIndex(KEY_NAME)));

 course.setTc(c.getInt(c.getColumnIndex(KEY_TC)));



 return course;

 }



 // lay ra danh sach tat ca cac course trong CSDL

 public ArrayList<Course> getAllCourse() {

 ArrayList<Course> arrCourse = new ArrayList<Course>();



 SQLiteDatabase database = this.getReadableDatabase();



 String selectQuerry = "SELECT * FROM " + TABLE_COURSE;



 LogUtil.LogD(LOG, selectQuerry);



 Cursor c = database.rawQuery(selectQuerry, null);



 if(c!=null) {

 // chuyen con tro den dong dau tien

 c.moveToFirst();

 }



 do {

 // dong goi du lieu vao doi tuong course

 Course course = new Course();

 course.set_id(c.getInt(c.getColumnIndex(KEY_ID)));

 course.setName(c.getString(c.getColumnIndex(KEY_NAME)));

 course.setTc(c.getInt(c.getColumnIndex(KEY_TC)));



 // them course vao danh sach

 arrCourse.add(course);

 } while (c.moveToNext()); // chuyen toi dong tiep theo



 // tra ve danh sach cac course

 return arrCourse;

 }



 // sua 1 course co ID = _id

 public void updateCourse(Course course, int _id) {

 SQLiteDatabase database = this.getWritableDatabase();



 ContentValues values = new ContentValues();

 values.put(KEY_NAME, course.getName());

 values.put(KEY_TC, course.getTc());



 database.update(TABLE_COURSE, values, KEY_ID + " = " + _id, null);

 }



 // xoa 1 course trong CSDL co ID = _id

 public void deleteCourse(int _id) {

 SQLiteDatabase database = this.getWritableDatabase();



 database.delete(TABLE_COURSE, KEY_ID + " = " + _id, null);

 }



 // them 1 quan he take vao CSDL

 public void insertTake(Student student, Course course) {

 SQLiteDatabase database = this.getWritableDatabase();



 ContentValues values = new ContentValues();

 values.put(KEY_STUDENT_ID, student.get_id());

 values.put(KEY_COURSE_ID, course.get_id());



 database.insert(TABLE_TAKE, null, values);

 }



 // lay ra thong tin 1 quan he take

 public Take getTake(int _id) {

 SQLiteDatabase database = this.getReadableDatabase();



 String selectQuerry = "SELECT * FROM " + TABLE_TAKE + " WHERE " + KEY_ID + " = " + _id;



 LogUtil.LogD(LOG, selectQuerry);



 Cursor c = database.rawQuery(selectQuerry, null);



 if(c!=null) {

 c.moveToFirst();

 }



 // dong goi du lieu vao doi tuong take

 Take take = new Take();

 take.set_id(c.getInt(c.getColumnIndex(KEY_ID)));



 Student student = getStudent(c.getInt(c.getColumnIndex(KEY_STUDENT_ID)));

 take.setStudent(student);



 Course course = getCourse(c.getInt(c.getColumnIndex(KEY_COURSE_ID)));

 take.setCourse(course);



 return take;

 }



 // lay ra danh sach tat ca quan he take

 public ArrayList<Take> getAllTake() {

 ArrayList<Take> arrTake = new ArrayList<Take>();



 SQLiteDatabase database = this.getReadableDatabase();



 String selectQuerry = "SELECT * FROM " + TABLE_TAKE;



 LogUtil.LogD(LOG, selectQuerry);



 Cursor c = database.rawQuery(selectQuerry, null);



 if(c!=null) {

 // chuyen con tro den dong dau tien

 c.moveToFirst();

 }



 do {

 // dong goi du lieu vao doi tuong take

 Take take = new Take();

 take.set_id(c.getInt(c.getColumnIndex(KEY_ID)));



 // get data tu bang voi SV co id c.getInt(c.getColumnIndex(KEY_STUDENT_ID))

 Student student = getStudent(c.getInt(c.getColumnIndex(KEY_STUDENT_ID)));

 take.setStudent(student);



 // get data tu bang courses voi course co id c.getInt(c.getColumnIndex(KEY_COURSE_ID))

 Course course = getCourse(c.getInt(c.getColumnIndex(KEY_COURSE_ID)));

 take.setCourse(course);



 // them take vao danh sach

 arrTake.add(take);

 } while (c.moveToNext()); // chuyen toi dong tiep theo



 // tra ve danh sach cac take

 return arrTake;

 }



 // sua 1 quan he take co ID = _id trong CSDL

 public void updateTake(Take take, int _id) {

 SQLiteDatabase database = this.getWritableDatabase();



 ContentValues values = new ContentValues();

 values.put(KEY_STUDENT_ID, take.getStudent().get_id());

 values.put(KEY_COURSE_ID, take.getCourse().get_id());



 database.update(TABLE_TAKE, values, KEY_ID + " = " + _id, null);

 }



 // xoa 1 quan he take co ID = _id trong CSDL

 public void deleteTake(int _id) {

 SQLiteDatabase database = this.getWritableDatabase();



 database.delete(TABLE_TAKE, KEY_ID + " = " + _id, null);

 }



public Cursor selectQuery(String selectQuery) {

 // thuc hien 1 cau select SQL bat ky

 // voi 1 bang trong CSDL

 // thong thuong minh hay viet ham nay de mem deo trong viec lay du lieu

 // thich hop voi cac ban da lam quen voi viec lay CSDL tu db

 SQLiteDatabase database = this.getWritableDatabase();



 LogUtil.LogD(LOG, selectQuery);

 return database.rawQuery(selectQuery, null);

 }



 // dong ket noi voi CSDL

 public void closeDatabase() {

 SQLiteDatabase database = this.getWritableDatabase();



 if(database!=null && database.isOpen()) {

 database.close();

 }

 }

}

Bước 9: file MainActivity.java

package com.hungvm.sqlitedemo3;



import java.util.ArrayList;

import java.util.HashMap;



import com.hungvm.sqlitedemo3.dbhelper.DatabaseHelper;

import com.hungvm.sqlitedemo3.model.Course;

import com.hungvm.sqlitedemo3.model.Student;

import com.hungvm.sqlitedemo3.model.Take;

import com.hungvm.sqlitetuto.R;



import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.widget.ListView;

import android.widget.SimpleAdapter;



public class MainActivity extends Activity {

 private DatabaseHelper dbHelper;



private ListView lvStudent;



 private SimpleAdapter adapter;



 private ArrayList<Take> arrTake;

 ArrayList<HashMap<String, String>> array;



 @Override

 protected void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);

 setContentView(R.layout.activity_main);

 dbHelper = new DatabaseHelper(MainActivity.this);

 lvStudent = (ListView) findViewById(R.id.lvStudents);



 loadData();



 listTake();



 // co the xu ly voi giao dien bat su kien click vao item list o duoi...

 // ...

 }



 private void loadData() {

 // tao du lieu demo cho vi du

 Student student = new Student("Manh Hung", "Ha Dong");

 dbHelper.insertStudent(student);



 student = new Student("Tuan Anh", "Cau Giay");

 dbHelper.insertStudent(student);



 student = new Student("Ngoc Lan", "Hai Ba Trung");

 dbHelper.insertStudent(student);



 Course course = new Course(5, "Co So Du Lieu");

 dbHelper.insertCourse(course);



 course = new Course(4 , "Cau truc du lieu");

 dbHelper.insertCourse(course);



 course = new Course(3 , "Lap trinh HDT");

 dbHelper.insertCourse(course);



 student = dbHelper.getStudent(1);

 course = dbHelper.getCourse(2);

 dbHelper.insertTake(student, course);



 student = dbHelper.getStudent(2);

 course = dbHelper.getCourse(3);

 dbHelper.insertTake(student, course);



 student = dbHelper.getStudent(3);

 course = dbHelper.getCourse(1);

 dbHelper.insertTake(student, course);

 }



 public void listTake() {



 // hien thi list cac take

 // lay du lieu tu cac bang trong CSDL hien thi ra listview

 arrTake = dbHelper.getAllTake();



 array = new ArrayList<HashMap<String,String>>();



 for(int i=0; i<arrTake.size(); i++) {

 HashMap<String, String> temp = new HashMap<String, String>();

 temp.put("st_name", arrTake.get(i).getStudent().getName());

 temp.put("st_id", String.valueOf(arrTake.get(i).getStudent().get_id()));

 temp.put("st_add", arrTake.get(i).getStudent().getAddress());

 temp.put("c_name", arrTake.get(i).getCourse().getName());

 temp.put("c_id", String.valueOf(arrTake.get(i).getCourse().get_id()));

 temp.put("c_tc", String.valueOf(arrTake.get(i).getCourse().getTc()));

 array.add(temp);

 }



 // init adapter

 String[] from = {

 "st_name", "st_id", "st_add", "c_name", "c_id", "c_tc"

 };



 int[] to = {

 R.id.tvStudentName, R.id.tvStudentId, R.id.tvStudentAdd, R.id.tvCourseName, R.id.tvCourseId, R.id.tvCourseTc

 };



 adapter = new SimpleAdapter(MainActivity.this, array,

 R.layout.layout_item, from, to);



 //

 lvStudent.setAdapter(adapter);

 }



}

Note:

Ví d ln này do mt em do mình hướng dn viết theo hướng dn ca mình, mình tôn trng bn quyn tác gi nên đ nguyên tên class và packet ch comment thêm 1 s ch cho các bn d hiu.

Các bn có th theo dõi kết qu thc hin chương trình trên logcat và màn hình. Khi làm theo ví d các bn có gì không hiu có th đ li câu hi trên web mình s tr li các bn mt cách nhanh nht có th. Các bn có th down full src theo link dưới đây

SQLiteDemo3

 

 HỖ TRỢ TRỰC TUYẾN