标签:姓名 span bst service name pos ext android tac
CursorAdapter继承于BaseAdapter,为Cursor和ListView连接提供了桥梁。
首先看一下CursorAdapter的部分源码:
/** * @see android.widget.ListAdapter# getView(int, View, ViewGroup) */ public View getView( int position, View convertView, ViewGroup parent) { if (!mDataValid) { throw new IllegalStateException( "this should only be called when the cursor is valid"); } if (!mCursor.moveToPosition(position)) { throw new IllegalStateException( "couldn‘t move cursor to position " + position); } View v; if (convertView == null) { v = newView( mContext, mCursor, parent); } else { v = convertView; } bindView(v, mContext, mCursor); return v; }
可以看出CursorAdapter是继承了BaseAdapter后覆盖它的getView方法在getView方法中调用了newView和bindView方法,我们在写CursorAdapter时必须实现它的两个方法。
public abstract View newView (Context context, Cursor cursor, ViewGroup parent); public abstract void bindView(View view, Context context, Cursor cursor);
继承CursorAdapter的示例部分代码:
@Override public View newView(Context context, Cursor cursor, ViewGroup parent) { ViewHolder viewHolder= new ViewHolder(); LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE ); View view=inflater.inflate(R.layout.item_contacts ,parent,false); viewHolder. tv_name=(TextView) view.findViewById(R.id.tv_showusername ); viewHolder. tv_phonenumber=(TextView) view.findViewById(R.id.tv_showusernumber ); view.setTag(viewHolder); Log. i("cursor" ,"newView=" +view); return view; } @Override public void bindView(View view, Context context, Cursor cursor) { Log. i("cursor" ,"bindView=" +view); ViewHolder viewHolder=(ViewHolder) view.getTag(); //从数据库中查询姓名字段 String name=cursor.getString(cursor.getColumnIndex(PersonInfo.NAME)); //从数据库中查询电话字段 String phoneNumber=cursor.getString(cursor.getColumnIndex(PersonInfo.PHONENUMBER)); viewHolder. tv_name.setText(name); viewHolder. tv_phonenumber.setText(phoneNumber); }
缺点:
cursor.getString(cursor.getColumnIndex("scheme"))
类似的代码.直接把Cursor暴露在ui层是很不好的示范,Cursor是非常底层的数据模型,不应该暴露在ui层。总之,CursorAdapter这种方式并不是很好的设计典范。
标签:姓名 span bst service name pos ext android tac
原文地址:http://www.cnblogs.com/lostland/p/6636007.html