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
[java]
view plaincopy
- <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