我有一个应用程序,将查询数据库,并试图输出一个列表格式的结果。
我的第一次尝试是使用simpleCursorAdapter,它可以很好地处理名称和状态等字符串输出,但不会从光标显示时间字段。我的数据库中的时间字段是Timestamp类型。时间字段的查询是
"strftime('%H:%M',time(sql_start_date,'unixepoch')) as start_time, " +
一点点的研究发现SimpleCorsorAdapter只适用于字符串。我发现这有点混乱,因为我认为字段start\u time的格式是字符串(毕竟我可以在日志窗口中查看结果,方法是:
Log.i(TAG,start_time);
所以我的下一个尝试是使用我自己的自定义游标适配器:
private class MyVisitsAdapter extends CursorAdapter{
private Cursor mCursor;
private Context mContext;
private final LayoutInflater mInflater;
public MyVisitsAdapter(Context context, Cursor cursor) {
super(context, cursor, true);
mInflater = LayoutInflater.from(context);
mContext = context;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
String st = cursor.getString(cursor.getColumnIndex(VisitsAdapter.KEY_CLIENT_START_TIME));
Log.i("Check Cursor result",st);
TextView t = (TextView) view.findViewById(R.id.start_time_display);
t.setText(st);
TextView t1 = (TextView) view.findViewById(R.id.end_time_display);
t.setText(cursor.getString(cursor.getColumnIndex(VisitsAdapter.KEY_CLIENT_END_TIME)));
t = (TextView) view.findViewById(R.id.name_entry);
t.setText(cursor.getString(cursor.getColumnIndex(VisitsAdapter.KEY_CLIENT_FULL_NAME)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = mInflater.inflate(R.layout.list_element, parent, false);
return view;
}
}
代码在以下行失败:
t.setText(st);
包含字段(列表)的xml文件_element.xml)具体如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android_id="@+id/start_time_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dip"
android:textColor="#333333"
android:layout_marginLeft="14px"
></TextView>
<TextView
android_id="@+id/end_time_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dip"
android:textColor="#333333"
android:layout_toRightOf="@+id/start_time_display"
android:layout_marginLeft="64px"></TextView>
<TextView
android:id="@+id/name_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dip"
android:textColor="#333333"
android:layout_marginLeft="94px"
android:layout_toRightOf="@+id/end_time_display"/>
<TextView
android:id="@+id/number_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textColor="#666666"
android:layout_marginLeft="14px"
android:layout_below="@+id/name_entry"
/>
</RelativeLayout>
希望我错过了一些简单的东西,一些有经验的人(我对android非常陌生,我的Java已经生锈了)
谢谢,这是预付款
基夫