liluvu / App / Creating a custom CursorAdapter for Lis...

分享

   

Creating a custom CursorAdapter for ListView ...

2010-12-23  liluvu

When populate data from database to a ListView, sometimes you want to do more than just map database column values to list row. At this situation, SimpleCursorAdapter is not suitable . Your need to create a custom CursorAdapter. The steps are simple:

  1. Subclass CursorAdapter
  2. Override bindView and newView

Code

row.xml
<script src="http://gist.github.com/241994.js?file=gistfile1.xsl"></script>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:src="@drawable/nba"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:layout_gravity="center_vertical">
<TextView
android:id="@+id/tv_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dayofyear"
android:gravity="center_horizontal"
android:textSize="14.0dip" />
<TextView
android:id="@+id/tv_week"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textSize="14.0dip" />
</LinearLayout>
<TextView
android:id="@+id/tv_time"
android:layout_width="50.0sp"
android:layout_height="wrap_content"
android:text="3"
android:gravity="right"
android:layout_gravity="center_vertical"
android:textSize="18.0dip"
android:layout_weight="1"/>
<TextView
android:id="@+id/tv_vs"
android:layout_width="140.0sp"
android:layout_height="wrap_content"
android:text="4" android:gravity="right"
android:layout_gravity="center_vertical"
android:textSize="18.0dip"
android:layout_weight="1"/>
</LinearLayout>
 
RowAdapter.java
<script src="http://gist.github.com/241993.js?file=gistfile1.java"></script>
class RowAdapter extends CursorAdapter {
 
private final LayoutInflater mInflater;
 
public RowAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
mInflater = LayoutInflater.from(context);
}
 
public RowAdapter(Context context, Cursor c) {
super(context, c);
mInflater = LayoutInflater.from(context);
}
 
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tvDate = (TextView) view.findViewById(R.id.tv_date);
TextView tvTime = (TextView) view.findViewById(R.id.tv_time);
TextView tvWeek = (TextView) view.findViewById(R.id.tv_week);
TextView tvVs = (TextView) view.findViewById(R.id.tv_vs);
 
StringBuilder date = new StringBuilder(cursor.getString(cursor
.getColumnIndex(DatabaseHelper.C_DATE)));
StringBuilder time = new StringBuilder(cursor.getString(cursor
.getColumnIndex(DatabaseHelper.C_TIME)));
String week = cursor.getString(cursor
.getColumnIndex(DatabaseHelper.C_WEEK));
String vs = cursor
.getString(cursor.getColumnIndex(DatabaseHelper.C_VS));
 
if (DateFormat.format("yyyyMMdd", System.currentTimeMillis())
.toString().equals(date.toString())) {
view.setBackgroundColor(Color.BLUE);
}
 
tvDate.setText(date.insert(4, '-').insert(7, '-'));
tvTime.setText(time.insert(time.length() - 2, ':'));
tvWeek.setText(week);
tvVs.setText(vs);
}
 
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = mInflater.inflate(R.layout.list_item, parent, false);
return view;
}
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。

    来自: liluvu > 《App》

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多
    喜欢该文的人也喜欢 更多

    ×
    ×

    ¥.00

    微信或支付宝扫码支付:

    开通即同意《个图VIP服务协议》

    全部>>