Android CursorAdapter Example |
Android CursorAdapter Example สำหรับ CursorAdapter จะใช้สำหรับการแสดงผลข้อมูลมาจาก SQLite หรือจาก Query ที่ได้จาก Resource ที่อยู่ใน Android ในรูปแบบของ Cursor
Cursor cursor = db.query(TABLE_COUNTRY, new String[] { "ID AS _id, *" },null, null, null, null, null);
การ Query บน SQLlite ซึ่งจะได้ Cursor ออกมา
รูปแบบของ CursorAdapter
public class myAdapter extends CursorAdapter
{
public myAdapter(Context context, Cursor cur) {
super(context, cur);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
}
public int getCount() {
// TODO Auto-generated method stub
return cursor.getCount();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
Example
ฐานข้อมูล SQLite
โครงสร้างของไฟล์ประกอบด้วย 3 ไฟล์คือ MainActivity.java, activity_main.xml และ activity_column.xml
activity_main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="ListView and CursorAdapter : "
android:layout_span="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<View
android:layout_height="1dip"
android:background="#CCCCCC" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
<View
android:layout_height="1dip"
android:background="#CCCCCC" />
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="By.. ThaiCreate.Com" />
</LinearLayout>
</TableLayout>
activity_column.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/ColID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="ID"/>
<TextView
android:id="@+id/ColCode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Code"/>
<TextView
android:id="@+id/ColCountry"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Country" />
</LinearLayout>
myDBClass.java
package com.myapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class myDBClass extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "mydatabase";
// Table Name
private static final String TABLE_COUNTRY = "country";
public myDBClass(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// Create Table Name
db.execSQL("CREATE TABLE " + TABLE_COUNTRY +
"(ID INTEGER PRIMARY KEY," +
" Code TEXT(100)," +
" Country TEXT(100));");
Log.d("CREATE TABLE","Create Table Successfully.");
}
// Insert Data
public long InsertData(String strID, String strCode, String strCountry) {
// TODO Auto-generated method stub
try {
SQLiteDatabase db;
db = this.getWritableDatabase(); // Write Data
/**
* for API 11 and above
SQLiteStatement insertCmd;
String strSQL = "INSERT INTO " + TABLE_MEMBER
+ "(ID,Code,Country) VALUES (?,?,?)";
insertCmd = db.compileStatement(strSQL);
insertCmd.bindString(1, strMemberID);
insertCmd.bindString(2, strName);
insertCmd.bindString(3, strTel);
return insertCmd.executeInsert();
*/
ContentValues Val = new ContentValues();
Val.put("ID", strID);
Val.put("Code", strCode);
Val.put("Country", strCountry);
long rows = db.insert(TABLE_COUNTRY, null, Val);
db.close();
return rows; // return rows inserted.
} catch (Exception e) {
return -1;
}
}
// Select Data
public Cursor SelectData() {
// TODO Auto-generated method stub
try {
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
Cursor cursor = db.query(TABLE_COUNTRY, new String[] { "ID AS _id, *" },null, null, null, null, null);
if(cursor != null)
{
return cursor;
}
else
{
return null;
}
} catch (Exception e) {
return null;
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_COUNTRY);
// Re Create on method onCreate
onCreate(db);
}
}
MainActivity.java
package com.myapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final myDBClass myDb = new myDBClass(this);
/** for Insert Statement
* myDb.InsertData("1", "TH", "Thailand");
myDb.InsertData("2", "VN", "Vietnam");
myDb.InsertData("3", "ID", "Indonesia");
myDb.InsertData("4", "LA", "Laos");
myDb.InsertData("5", "MY", "Malaysia");
*/
// listView1
final ListView lisView1 = (ListView)findViewById(R.id.listView1);
Cursor c = myDb.SelectData();
lisView1.setAdapter(new CountryAdapter(this, c));
}
public class CountryAdapter extends CursorAdapter
{
private Cursor cursor;
public CountryAdapter(Context context, Cursor cur) {
super(context, cur);
cursor = cur;
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
// ColID
TextView txtID = (TextView) view.findViewById(R.id.ColID);
txtID.setText(cursor.getString(cursor.getColumnIndex("ID")));
// ColCode
TextView txtCode = (TextView) view.findViewById(R.id.ColCode);
txtCode.setText(cursor.getString(cursor.getColumnIndex("Code")));
// ColCountry
TextView txtCountry = (TextView) view.findViewById(R.id.ColCountry);
txtCountry.setText(cursor.getString(cursor.getColumnIndex("Country")));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.activity_column, parent, false);
bindView(view, context, cursor);
return view;
}
public int getCount() {
// TODO Auto-generated method stub
return cursor.getCount();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
/* or used this getView()
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
CountryHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.activity_column, null);
holder = new CountryHolder();
holder.ID = (TextView) convertView.findViewById(R.id.ColID);
holder.Code = (TextView) convertView.findViewById(R.id.ColCode);
holder.Country = (TextView) convertView
.findViewById(R.id.ColCountry);
convertView.setTag(holder);
} else {
holder = (CountryHolder) convertView.getTag();
}
cursor.moveToPosition(position);
holder.ID.setText(cursor.getString(cursor.getColumnIndex("ID")));
holder.Code.setText(cursor.getString(cursor.getColumnIndex("Code")));
holder.Country.setText(cursor.getString(cursor.getColumnIndex("Country")));
return convertView;
}
*/
}
public class CountryHolder {
TextView ID;
TextView Code;
TextView Country;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Screenshot
CursorAdapter Example
สำหรับการใช้งาน Cursor อื่น ๆ สามารถศึกษาได้จากบทความของ SQLite กับ Android
อ่านเพิ่มเติมเกี่ยวกับ CursorAdapter
http://developer.android.com/reference/android/widget/CursorAdapter.html
Property & Method (Others Related) |
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2012-08-11 16:40:35 /
2012-08-14 17:35:44 |
|
Download : |
No files |
|
Sponsored Links / Related |
|
|
|
|
|
|
|