Android之CursorAdapter用法
来源:互联网 发布:js预览上传图片 编辑:程序博客网 时间:2024/06/10 15:11
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
- Android之CursorAdapter用法
- Android之CursorAdapter用法
- Android之CursorAdapter用法
- Android之CursorAdapter用法
- Android之CursorAdapter用法
- Android之CursorAdapter用法
- Android之CursorAdapter示例
- Android适配器之CursorAdapter
- Android使用之CursorAdapter
- Android 中 CursorAdapter 的用法
- Android基础之CursorAdapter 的用法与获取联系人信息显示、并点击拨号
- Android CursorAdapter
- android之ListView中使用的适配器CursorAdapter
- Android开发之通过CursorAdapter读取数据(源代码分享)
- Android 使用自定义cursorAdapter
- 【android】使用CursorAdapter注意事项
- Android CursorAdapter的使用
- Android中的CursorAdapter
- Linux下安装Eclipse的PHP插件
- 架构在项目中所起的作用
- 为你开发的android应用apk签名
- 后终端时代的移动开发
- 电脑连接电视的方法---HDMI篇
- Android之CursorAdapter用法
- eclipseIDE for Java EE 下 离线 安装CDT的方法
- 自己的 「画面文言修正 check list」
- ERROR: Application requires API version 10. Device API version is 8
- tomcat6.0配置(含配置视频下载)
- 推荐系统的循序进阶读物(从入门到精通)
- VC中如何使窗口居中
- IT运维都干什么
- 利用tcpcopy引流做模拟在线测试