Android之CursorAdapter用法

來源:互聯網
上載者:User

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
  1. <span style="font-size:16px;">mport java.util.List;  
  2. import android.app.Activity;  
  3. import android.app.ListActivity;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.content.Context;  
  7. import android.content.ContentValues;  
  8. import android.database.Cursor;  
  9. import android.view.LayoutInflater;  
  10. import android.view.View;  
  11. import android.widget.ListView;  
  12. import android.view.ViewGroup;  
  13. import android.widget.ArrayAdapter;  
  14. import android.widget.CursorAdapter;  
  15. import android.widget.TextView;  
  16. import android.provider.ContactsContract.Contacts;  
  17. import android.provider.ContactsContract.RawContacts;  
  18. import android.view.View.OnClickListener;       
  19. import android.widget.Button;   
  20. public class HelloCursor extends ListActivity {  
  21.     private static String[] PROJECTION = new String[] { Contacts._ID,  
  22.             Contacts.DISPLAY_NAME };  
  23.   
  24.     /** Called when the activity is first created. */  
  25.     @Override  
  26.     public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.main);  
  29.         Cursor c = getContentResolver().query(Contacts.CONTENT_URI, PROJECTION,  
  30.                 null, null, Contacts.DISPLAY_NAME + " COLLATE NOCASE");  
  31.         startManagingCursor(c);  
  32.         MyCursorAdapter adapter = new MyCursorAdapter(this, R.layout.list_row,  
  33.                 c);  
  34.         this.setListAdapter(adapter);  
  35.         Button button = (Button)findViewById(R.id.Button01);   
  36.         OnClickListener listener=new OnClickListener(){  
  37.             @Override      
  38.             public void onClick(View v) {       
  39.                 doAction();       
  40.             }           
  41.         };  
  42.         button.setOnClickListener(listener);  
  43.         mHandler = new Handler();  
  44.          
  45.     }  
  46.   
  47.     private String[] mStrings = { "hubin", "hudashi", "robin" };  
  48.     int cnt = 0;  
  49.      private Handler mHandler;  
  50.       
  51.     class AddContactThread implements Runnable {  
  52.         public void run() {  
  53.             int nStringLength = mStrings.length;  
  54.             int randomNumber = 0;  
  55.             ContentValues newValues = new ContentValues();  
  56.             String tempString = null;  
  57.             randomNumber = (int) (Math.random() % 10);  
  58.             for (int i = 0; i < nStringLength; i++) {  
  59.                 tempString = mStrings + cnt + randomNumber;  
  60.                 newValues.put(Contacts.DISPLAY_NAME, tempString);  
  61.                 getContentResolver().insert(RawContacts.CONTENT_URI, newValues);  
  62.                 newValues.clear();  
  63.   
  64.             }  
  65.             cnt++;  
  66.         }  
  67.     }  
  68.     AddContactThread addContact=new AddContactThread();  
  69.     void doAction()  
  70.     {  
  71.          mHandler.post(addContact);  
  72.     }  
  73. }  
  74. class MyCursorAdapter extends CursorAdapter {  
  75.     Context  context=null;  
  76.     int viewResId;  
  77.     public MyCursorAdapter(Context context, int resource, Cursor cursor) {  
  78.         super(context,cursor);  
  79.         viewResId=resource;  
  80.     }  
  81.     public View newView(Context context, Cursor cursor, ViewGroup parent) {  
  82.           
  83.         TextView view =null;  
  84.        LayoutInflater vi = null;  
  85.        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  86.        view =(TextView)vi.inflate(viewResId, parent, false);  
  87.            //v =(TextView)vi.inflate(textViewResourceId,null);  
  88.        Log.i("hubin","newView"+view);  
  89.         return view;  
  90.     }  
  91.     @Override  
  92.     public void bindView(View view, Context context, Cursor cursor) {  
  93.         Log.i("hubin","bind"+view);  
  94.         TextView nameView = (TextView) view;  
  95.         // Set the name  
  96.         nameView.setText(cursor  
  97.                 .getString(cursor.getColumnIndex("DISPLAY_NAME")));  
  98.     }  
  99. }</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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.