示例一
简单的例子说明一下,使用CursorAdapter读取数据库里,需要的数据,把它显示到listview上。
ListView数据来自数据库
CursorAdapter 继承了 BaseAdapter
好处:仅加载需要显示的数据,性能好
使用方法:
1.实现两个参数构造方法
2.重写newView()方法
layout->view
3.重写bindView()方法
view.set
数据更新:
adapter.changeCursor(cursor);
adapter.notifyDataSetChanged()
代码如下:
01.
public
class
MySqliteOpenhelper
extends
SQLiteOpenHelper
02.
{
03.
04.
public
MySqliteOpenhelper(Context context,
int
version)
05.
{
06.
super
(context,
"dianhuaben.db"
,
null
, version);
07.
}
08.
@Override
09.
public
void
onCreate(SQLiteDatabase db)
10.
{
//注意:使用CursorAdapter时,创建表必须有以_id为列名的列
11.
String sql =
"CREATE TABLE dhb (_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))"
;
12.
db.execSQL(sql);
13.
}
14.
@Override
15.
public
void
onUpgrade(SQLiteDatabase db,
int
oldVersion,
int
newVersion)
16.
{
17.
}
18.
}
使用CursorAdapter的代码如下:
01.
public
void
createCursorAdapter(Cursor cursor)
02.
{
//游标适配器,构造方法,传入cursor
03.
mAdapter =
new
CursorAdapter(
this
, cursor)
04.
{
//重写两个方法
05.
@Override
06.
public
View newView(Context context, Cursor cursor, ViewGroup parent)
07.
{
//找到布局和控件
08.
ViewHolder holder =
new
ViewHolder();
09.
LayoutInflater inflater = getLayoutInflater();
10.
View inflate = inflater.inflate(R.layout.listview_item,
null
);
11.
holder.item_tv_name = (TextView) inflate.findViewById(R.id.item_tv_name);
12.
holder.item_tv_phone = (TextView) inflate.findViewById(R.id.item_tv_phone);
13.
inflate.setTag(holder);
14.
return
inflate;
//返回的view传给bindView。
15.
}
16.
17.
@Override
18.
public
void
bindView(View view, Context context, Cursor cursor)
19.
{
//复用布局。www.it165.net
20.
// 把数据设置到界面上
21.
ViewHolder holder = (ViewHolder) view.getTag();
22.
String name = cursor.getString(cursor.getColumnIndex(
"name"
));
23.
String phone = cursor.getString(cursor.getColumnIndex(
"phone"
));
24.
holder.item_tv_name.setText(name);
25.
holder.item_tv_phone.setText(phone);
26.
}
27.
28.
};
29.
30.
};
另一种是使用SimpleCursorAdapter,简化了代码,同样达到如上的效果
SimpleCursorAdapter继承了CursorAdapter,用法类似SimpleAdapter。
代码如下:
1.
public
void
createSimpleCursorAdapter(Cursor cursor)
2.
{
// SimpleCursorAdapter继承了CursorAdapter继承了BaseAdapter
3.
String[] from = {TABLE_NAME_NAME,TABLE_NAME_PHONE};
//列名与控件id一一对应
4.
int
[] to = {R.id.item_tv_name,R.id.item_tv_phone};
5.
//用的是SimpleCursorAdapter,用法和simpleAdapter相似
6.
mAdapter =
new
SimpleCursorAdapter(MainActivity.
this
, R.layout.listview_item, cursor, from, to);
7.
}
注意:使用SimpleCursorAdapter,时,创建表必须有以_id为列名的列
示例二
CursorAdapter
继承于BaseAdapter是个虚类,它为cursor和ListView提供了连接的桥梁。
public abstract class
CursorAdapter
extends BaseAdapter
直接子类只有ResourceCursorAdapter
Class Overview
Adapter that exposes data from a Cursor to a ListView widget.
The Cursor must include a column named "_id" or this class will not work.
注意cursor的必须要有个命名为" _id "的列。比如Contacts._ID就为" _id "
必须实现以下函数
:
abstract View newView (Context context, Cursor cursor, ViewGroup parent)
Makes a new view to hold the data pointed to by cursor.
abstract void bindView (View view, Context context, Cursor cursor)
Bind an existing view to the data pointed to by cursor
注意:
newView该函数第一次回调用后,如果数据增加后也会再调用,但是重绘是不会调用的。
数据增加后,回调用该函数来生成与新增数据相对应的view。
bindView函数第一次回调用后,如果数据更新也会再调用,但重绘会再次调用的。
【总的来说应该是在调用bindView如果发现view为空会先调用newView来生成view】
i
附1:关于newView和bindView一测试结果
newView android.widget.TextView@43b98ea0
bind android.widget.TextView@43b98ea0
newView android.widget.TextView@43b99948
bind android.widget.TextView@43b99948
newView android.widget.TextView@43b9a3f0
bind android.widget.TextView@43b9a3f0
add
bind android.widget.TextView@43b9a3f0
bind android.widget.TextView@43b99948
bind android.widget.TextView@43b98ea0
newView android.widget.TextView@43b9c5b0
bind android.widget.TextView@43b9c5b0
newView android.widget.TextView@43b9d058
bind android.widget.TextView@43b9d058
newView android.widget.TextView@43b9db00
bind android.widget.TextView@43b9db00
public abstract class
CursorAdapter
extends BaseAdapter
直接子类只有ResourceCursorAdapter
Class Overview
Adapter that exposes data from a Cursor to a ListView widget.
The Cursor must include a column named "_id" or this class will not work.
注意cursor的必须要有个命名为" _id "的列。比如Contacts._ID就为" _id "
abstract View newView (Context context, Cursor cursor, ViewGroup parent)
Makes a new view to hold the data pointed to by cursor.
abstract void bindView (View view, Context context, Cursor cursor)
Bind an existing view to the data pointed to by cursor
注意:
newView该函数第一次回调用后,如果数据增加后也会再调用,但是重绘是不会调用的。
数据增加后,回调用该函数来生成与新增数据相对应的view。
bindView函数第一次回调用后,如果数据更新也会再调用,但重绘会再次调用的。
【总的来说应该是在调用bindView如果发现view为空会先调用newView来生成view】
i
- <span style="font-size:16px;">mport java.util.List;
- import android.app.Activity;
- import android.app.ListActivity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.content.Context;
- import android.content.ContentValues;
- import android.database.Cursor;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.ListView;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.CursorAdapter;
- import android.widget.TextView;
- import android.provider.ContactsContract.Contacts;
- import android.provider.ContactsContract.RawContacts;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class HelloCursor extends ListActivity {
- private static String[] PROJECTION = new String[] { Contacts._ID,
- Contacts.DISPLAY_NAME };
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Cursor c = getContentResolver().query(Contacts.CONTENT_URI, PROJECTION,
- null, null, Contacts.DISPLAY_NAME + " COLLATE NOCASE");
- startManagingCursor(c);
- MyCursorAdapter adapter = new MyCursorAdapter(this, R.layout.list_row,
- c);
- this.setListAdapter(adapter);
- Button button = (Button)findViewById(R.id.Button01);
- OnClickListener listener=new OnClickListener(){
- @Override
- public void onClick(View v) {
- doAction();
- }
- };
- button.setOnClickListener(listener);
- mHandler = new Handler();
- }
- private String[] mStrings = { "hubin", "hudashi", "robin" };
- int cnt = 0;
- private Handler mHandler;
- class AddContactThread implements Runnable {
- public void run() {
- int nStringLength = mStrings.length;
- int randomNumber = 0;
- ContentValues newValues = new ContentValues();
- String tempString = null;
- randomNumber = (int) (Math.random() % 10);
- for (int i = 0; i < nStringLength; i++) {
- tempString = mStrings + cnt + randomNumber;
- newValues.put(Contacts.DISPLAY_NAME, tempString);
- getContentResolver().insert(RawContacts.CONTENT_URI, newValues);
- newValues.clear();
- }
- cnt++;
- }
- }
- AddContactThread addContact=new AddContactThread();
- void doAction()
- {
- mHandler.post(addContact);
- }
- }
- class MyCursorAdapter extends CursorAdapter {
- Context context=null;
- int viewResId;
- public MyCursorAdapter(Context context, int resource, Cursor cursor) {
- super(context,cursor);
- viewResId=resource;
- }
- public View newView(Context context, Cursor cursor, ViewGroup parent) {
- TextView view =null;
- LayoutInflater vi = null;
- vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- view =(TextView)vi.inflate(viewResId, parent, false);
- //v =(TextView)vi.inflate(textViewResourceId,null);
- Log.i("hubin","newView"+view);
- return view;
- }
- @Override
- public void bindView(View view, Context context, Cursor cursor) {
- Log.i("hubin","bind"+view);
- TextView nameView = (TextView) view;
- // Set the name
- nameView.setText(cursor
- .getString(cursor.getColumnIndex("DISPLAY_NAME")));
- }
- }</span>
附1:关于newView和bindView一测试结果
newView android.widget.TextView@43b98ea0
bind android.widget.TextView@43b98ea0
newView android.widget.TextView@43b99948
bind android.widget.TextView@43b99948
newView android.widget.TextView@43b9a3f0
bind android.widget.TextView@43b9a3f0
add
bind android.widget.TextView@43b9a3f0
bind android.widget.TextView@43b99948
bind android.widget.TextView@43b98ea0
newView android.widget.TextView@43b9c5b0
bind android.widget.TextView@43b9c5b0
newView android.widget.TextView@43b9d058
bind android.widget.TextView@43b9d058
newView android.widget.TextView@43b9db00
bind android.widget.TextView@43b9db00