使用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
     }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值