Adapter(15)自定义CursorAdapter

自定义CursorAdapter很简单

实现两个函数就可,主要工作在Cursor。

  • newView 为每行返回一个view
  • bindView 为每行绑定数据

示例如下:

 1 import android.content.Context;
 2 import android.database.Cursor;
 3 import android.view.LayoutInflater;
 4 import android.view.View;
 5 import android.view.ViewGroup;
 6 import android.widget.CursorAdapter;
 7 import android.widget.TextView;
 8 
 9 public class CustomCursorAdapter extends CursorAdapter {
10 
11     public CustomCursorAdapter(Context context, Cursor c, int flags) {
12         super(context, c, flags);
13     }
14 
15     public CustomCursorAdapter(Context context, Cursor c, boolean autoRequery) {
16         super(context, c, autoRequery);
17     }
18 
19     public CustomCursorAdapter(Context context, Cursor c) {
20         super(context, c);
21     }
22 
23     //为当前行返回一个view
24     @Override
25     public View newView(Context context, Cursor cursor, ViewGroup parent) {
26         LayoutInflater inflater = LayoutInflater.from(context);
27         View v = inflater.inflate(android.R.layout.simple_list_item_1, parent,false);
28         return v;
29     }
30     
31     //设置当前行的数据
32     @Override
33     public void bindView(View view, Context context, Cursor cursor) {
34         String text = "";
35         int ct = cursor.getColumnCount();
36         for (int i = 0; i < ct; i++) {
37             text += cursor.getColumnName(i);
38             text += ", ";
39         }
40         TextView tv = (TextView) view.findViewById(android.R.id.text1);
41         tv.setText(text);
42     }
43 
44 }

 

posted @ 2015-11-27 23:48  f9q  阅读(136)  评论(0)    收藏  举报