【发布时间】:2010-08-10 11:44:01
【问题描述】:
我有一个应用程序可以查询数据库并尝试以列表格式输出结果。
我的第一次尝试是使用 simpleCursorAdapter,它对名称和状态等字符串输出效果很好,但不会显示光标的时间字段。我数据库中的时间字段是时间戳类型。时间字段的查询是
"strftime('%H:%M',time(sql_start_date,'unixepoch')) as start_time, " +
稍作研究发现 SimpleCursorAdapters 仅适用于字符串。我发现这有点令人困惑,因为我会认为字段 start_time 是字符串格式(毕竟我可以通过以下方式在日志窗口中查看结果:
Log.i(TAG,start_time);
所以我的下一个尝试是使用我自己的自定义 CursorAdapter:
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);
出现空指针异常。适配器未找到“start_time_display”文本视图。
带有字段(list_element.xml)的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>
如您所见,其中清楚地包含“start_time_display”文本视图。
希望我错过了一些有更多经验的人的简单东西(我对 android 很陌生,我的 Java 很生锈)
先谢谢了
凯夫
【问题讨论】:
标签: java android listview cursor