首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

运用CursorAdapter自定义Cursor和ListView之间的数据绑定

2012-08-30 
使用CursorAdapter自定义Cursor和ListView之间的数据绑定??? 最近在做一个短信应用,需要将系统的短信数据

使用CursorAdapter自定义Cursor和ListView之间的数据绑定

??? 最近在做一个短信应用,需要将系统的短信数据读取出来,并显示在listview中,但是由于短信数据本身并不是十分适合于软件用户直接阅读。必须将短信内容适当修改后才能更为用户接受。刚开始我用的SimpleAdapter实现,但是后来发现使用SimpleAdapter实现的话,列表的渲染速度很慢,于是决定改用SimpleCursorAdapter,但是刚开始时不知道怎么将数据修改后添加到lsitview中。后来发现重写CursorAdapter中的newView()和bindView()方法即可自定义数据和组件的绑定过程,更重要的是种方法绑定效率十分的高。下面是部分主要过程及代码:

??? 1、得到要绑定的cursor对象,我使用managedQuery()方法得到。

??? 2、写一个继承自CursorAdapter抽象类的类,并重写其中的bindView()、newView()及构造方法。

??? class myCursorAdapter extends CursorAdapter {
??? ?private LayoutInflater mInflater;
??? ?private Context mContext;
??? ?TextView add_con;
?????public myCursorAdapter(Context context, Cursor c) {
?????? ??super(context, c);
?????? ??mContext = context;
????? ???mInflater = LayoutInflater.from(context);
? ???}
? ???@Override
?? ??public void bindView(View view, Context context, Cursor cursor) {
??? ? ???String body = cursor.getString(cursor.getColumnIndex("body"));
????? ???String date = cursor.getString(cursor.getColumnIndex("date"));
?????????TextView address_TV = (TextView)view.findViewById(R.id.TelNumber_Content);
??? ?? ??TextView dateString_TV = (TextView)view.findViewById(R.id.Time_Date);
??? ?? ??address_TV.setText("我" + ":" + body);
??? ? ???dateString_TV.setText(date);
?????}

??? ?@Override
?? ??public View newView(Context context, Cursor cursor, ViewGroup parent) {
?????????return mInflater.inflate(R.layout.conversation_list_item,parent,false);??//一般都这样写,返回列表行元素,注意这里返回的就是bindView中的view
?????}

?

热点排行