Nội dung:
- Tabs
- Custom adapter
--------------
1. Tabs:
- Sử dụng tabhost tạo ví dụ như sau
- Download icon của dự án:
https://drive.google.com/file/d/0B8tAQ0_sJKCSOExhcFpFY1VjTXM/view?usp=sharing
- Cấu trúc dự án:
- Mã nguồn XML: 4 tệp tin
  + Tệp tin activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/linelayout"       
       android:orientation="vertical"
        android:layout_width="match_parent"
       android:layout_height="match_parent">
       <TabWidget
           android:id="@android:id/tabs"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"/>
              <FrameLayout
                  android:id="@android:id/tabcontent"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"/>
       </LinearLayout>     
</TabHost>
  + Tệp tin photoslayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewPhotos"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="15dp"
        android:text="Photos here"
        android:textSize="18sp" />
</LinearLayout>
  + Tệp tin songslayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewSongs"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="15dp"
        android:text="Songs here"
        android:textSize="18sp" />

</LinearLayout>
  + Tệp tin videoslayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewVideos"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="15dp"
        android:text="Videos here"
        android:textSize="18sp" />

</LinearLayout>

Tạo 3 tệp tin XML trong thư mục drawable: 3 tệp tin
  + Tệp tin icon_photos_tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/photos_gray"
          android:state_selected="true"/>
    <item android:drawable="@drawable/photos_white"/>
</selector>
  + Tệp tin icon_songs_tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/songs_gray"
          android:state_selected="true"/>
    <item android:drawable="@drawable/songs_white"/>
</selector>
  + Tệp tin icon_videos_tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/videos_gray"
          android:state_selected="true"/>
    <item android:drawable="@drawable/videos_white"/>
</selector>

- Mã nguồn java:
  + Tệp tin PhotosActivity.java:
package com.tabs;

import android.app.Activity;
import android.os.Bundle;

public class PhotosActivity extends Activity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.photoslayout);
       }
}
  + Tệp tin SongsActivity.java:
package com.tabs;

import android.app.Activity;
import android.os.Bundle;

public class SongsActivity extends Activity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.songslayout);
       }
}
  + Tệp tin VideosActivity.java:
package com.tabs;

import android.app.Activity;
import android.os.Bundle;

public class VideosActivity extends Activity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.videoslayout);
       }
}
  + Tệp tin MainActivity.java:
package com.tabs;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
             
              TabHost tabhost = getTabHost();
             
              //Tabhost photos
              TabSpec photosSpec = tabhost.newTabSpec("Photos");
              photosSpec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
              Intent photosIntent = new Intent(this, PhotosActivity.class);
              photosSpec.setContent(photosIntent);
             
              //Tabhost songs
              TabSpec songsSpec = tabhost.newTabSpec("Songs");
              songsSpec.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_songs_tab));
              Intent songsIntent = new Intent(this, SongsActivity.class);
              songsSpec.setContent(songsIntent);
             
              //Tabhost videos
              TabSpec videosSpec = tabhost.newTabSpec("Videos");
              videosSpec.setIndicator("Videos", getResources().getDrawable(R.drawable.icon_videos_tab));
              Intent videosIntent = new Intent(this, VideosActivity.class);
              videosSpec.setContent(videosIntent);
                          
              //Add all TabSpec to TabHost
              tabhost.addTab(photosSpec);
              tabhost.addTab(songsSpec);
              tabhost.addTab(videosSpec);
       }
}

- Tệp tin AndroidMainifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tabs"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".PhotosActivity"/>
        <activity android:name=".SongsActivity"/>
        <activity android:name=".VideosActivity"/>
    </application>

</manifest>

2. Custom adapter:
- Xem bài 10, phần 3: http://huongdan-laptrinh.blogspot.com/2015/08/bai-10-how-to-work-with-threads-files.html

Bài tập:
1.

2.
Hướng dẫn sử dụng SQLite:
Sử dụng công cụ SQLite Administrator để tạo một CSDL: link download bên dưới:
https://drive.google.com/file/d/0B8tAQ0_sJKCSRkJoOS1NRUl5bGM/view?usp=sharing

Xem hướng dẫn về các câu lệnh cơ bản của sqlite:
http://www.thegeekstuff.com/2012/09/sqlite-command-examples/

Các chức năng cơ bản để thao tác với SQLite Android gồm:
1) Cách tạo / xóa một cơ sở dữ liệu:
SQLiteDatabase db = openOrCreateDatabase("NameDataBase.db", Context.MODE_PRIVATE, null);
2) Cách tạo / xóa bảng trong SQLite:
String sql = "CREATE TABLE IF NOT EXISTS student(rollno VARCHAR, name VARCHAR, marks VARCHAR);";
db.execSQL(sql);
Trong đó: student là tên bảng cần tạo
          rollno, name, marks: là tên các trường (các cột) trong bảng
          VARCHAR là kiểu dữ liệu
3) Cách thêm/ sửa/ xóa dữ liệu trong bảng:
Thêm dữ liệu:
String sql = "INSERT INTO student VALUES('"Cột 1"','"Cột 2"','"Cột 3"');";
db.execSQL(sql);
Sửa dữ liệu:
String sql = "UPDATE student SET name='"+value1+"',marks='"+value2+"'
             WHERE rollno='"+value3+"'";
db.execSQL(sql);
Trong đó: name, markslà tên cột trong bảng, value1, value2là các giá trị cần thay đổi
Xóa dữ liệu


4) Cách truy vấn dữ liệu trong bảng:
db.execSQL(sql);


Viết ứng dụng lưu danh bạ điện thoại như sau:
Cấu trúc dự án:

Mã nguồn:
+ Tệp tin DatabaseHandler.java:
package com.contacts_sqlite;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper {
       private static final int DATABASE_VERSION = 1;
       private static final String DATABASE_NAME = "constactsManager";
       private static final String TABLE_CONTACTS = "contacts";
      
       //Table culums names
       private static final String KEY_ID = "id";
       private static final String KEY_NAME = "name";
       private static final String KEY_PHONE = "phone";
      
       public DatabaseHandler(Context context) {
              super(context, DATABASE_NAME, null, DATABASE_VERSION);
       }
      
       @Override
       public void onCreate(SQLiteDatabase db) {
              String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                           + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                           + KEY_PHONE + " TEXT" + ")";
              db.execSQL(CREATE_CONTACTS_TABLE);
       }
      
       @Override
       public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
              db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
              onCreate(db);
       }
      
       //add new contact
       void addContact(Person person) {
              SQLiteDatabase db = this.getWritableDatabase();
             
              ContentValues values = new ContentValues();
              values.put(KEY_NAME, person.GetName());
              values.put(KEY_PHONE, person.GetPhone());
              //insert to database
              db.insertOrThrow(TABLE_CONTACTS, null, values);
              db.close();
       }
      
       Person getPerson(int id) {
              SQLiteDatabase db = this.getReadableDatabase();
             
              Cursor cursor = db.query( TABLE_CONTACTS, new String[] {KEY_ID, KEY_NAME, KEY_PHONE}, KEY_ID + "=?", new String[] {String.valueOf(id)}, null, null, null, null );
              if (cursor != null) {
                     cursor.moveToFirst();
              }
             
              Person person = new Person(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2));
              return person;
       }
      
       //get all contacts
       public List<Person> getAllContacts() {
              List<Person> contactslist = new ArrayList<Person>();
              String selectAllQuery = "SELECT * FROM " + TABLE_CONTACTS;
             
              SQLiteDatabase db = this.getWritableDatabase();
              Cursor cursor = db.rawQuery(selectAllQuery, null);
                    
              if (cursor.moveToFirst()) {
                     do {
                           Person person = new Person();
                           person.SetId(Integer.parseInt(cursor.getString(0)));
                           person.SetName(cursor.getString(1));
                           person.SetPhone(cursor.getString(2));
                           contactslist.add(person);
                     } while(cursor.moveToNext());
              }
             
              return contactslist;
       }
}

+ Tệp tin MainActivity.java:
package com.contacts_sqlite;

import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

public class MainActivity extends Activity {
       private ListView myListView;
       private MyAdapterArray myAdapter;
       private List<Person> personArray;
       public static final int REQUEST_CODE_ADD = 2;
             
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
             
              myListView = (ListView) findViewById(R.id.myListView);
             
              DatabaseHandler db = new DatabaseHandler(this);       
              db.addContact(new Person("Nguyen Van A", "0905686868"));
              db.addContact(new Person("Nguyen Van B", "0903555999"));
              db.addContact(new Person("Nguyen Van C", "0985355355"));

              // create ArrayList
              personArray = db.getAllContacts();       
              myAdapter = new MyAdapterArray(this, R.layout.list_item, personArray);
              myListView.setAdapter(myAdapter);
       }

       public void onClickAdd(View v) {
              //create the intent
              Intent intent = new Intent(MainActivity.this, AddActivity.class);         
              //start the intent
              this.startActivityForResult(intent, REQUEST_CODE_ADD);
       }
      
       @Override
       protected void onActivityResult(int requestCode, int resultCode, Intent data)   
      {      
              if(requestCode==REQUEST_CODE_ADD)
              {
                     DatabaseHandler db = new DatabaseHandler(this);
                     personArray = db.getAllContacts();
                     myAdapter.notifyDataSetChanged();
              }
       }
       //….  
}

+ Tệp tin Person.java:
package com.contacts_sqlite;

public class Person {
       int id;
       private String name, phone;
      
       public Person() {
      
       }

       public Person(String _name, String _phone) {
              this.name = _name;
              this.phone = _phone;
       }
      
       public Person(int _id, String _name, String _phone) {
              this.id = _id;
              this.name = _name;
              this.phone = _phone;
       }
      
       public int GetId() {
              return this.id;
       }
      
       public void SetId(int _id) {
             
              this.id = _id;
       }
      
       public void SetName(String _name) {
              this.name = _name;
       }
      
       public String GetName() {
              return name;
       }
      
       public void SetPhone(String _phone) {
              this.phone = _phone;
       }
      
       public String GetPhone() {
              return phone;
       }
}

+