cursorAdapter

 

cursorAdapter是虚类,继承需要实现两个抽象方法.

下面是关于 bindView()和newView()两个抽象方法需要实现的内容。

  • public void bindView(View view, Context context, Cursor cursor)
    重用一个已有的view,使其显示当前cursor所指向的数据。
  • public View newView(Context context, Cursor cursor, ViewGroup parent)
    为cursor所指向的数据新建一个View对象,并显示其数据。
  • //类似平时我们使用的adapter的getview里面的这样两段代码
  • 	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		if (convertView == null) {
    			LayoutInflater inflater = (LayoutInflater) context
    					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
    			convertView = inflater.inflate(layout_id_lc, null);
    			viewHolder = new ViewHolder();
    			viewHolder.cometTExtview = (TextView) convertView
    					.findViewById(ListItemId[0]);
    			 
    			convertView.setTag(viewHolder);
    		} else {
    			viewHolder = (ViewHolder) convertView.getTag();
    		}                                                                                        return convertView;}



  • 示例代码如下:

class MyCursorAdapter extends CursorAdapter {
	String TAG=this.getClass().getName();
	Context context = null;
	int viewResId;

	public MyCursorAdapter(Context context, int LayoutID, Cursor cursor) {
		super(context, cursor);
		viewResId = LayoutID;
	}

	public View newView(Context context, Cursor cursor, ViewGroup parent) {

		Log.i(TAG, "in new view");
		View view = null;
		LayoutInflater mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		view = (View) mInflater.inflate(viewResId, parent, false);		
		return view;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		 
		Log.i(TAG, "in bind view");
		TextView nameView = (TextView) view; 
		nameView.setText(  cursor.getString(cursor.getColumnIndex("DISPLAY_NAME"))  );
	}	
	
}

 在实际的应用编写过程中,需要通过适配器(Adapter)来将Cursor与适配器控件联系起来。

Android为Cursor提供了的抽象类 CursorAdapter,就可以方便实现Cursor与适配器的连接。

但这个似乎不是很好的隐藏数据的感觉,毕竟他暴露了低层数据库实现的具体内容.

推荐使用 一些常用的封装好的库来操作.例如 ormlite  ..greendao ..androrm   详情见 请点击我 



Cursor-Class



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值