I have an app that will query a database and attempt to output results in a list format.
my first attempt was to use a simpleCursorAdapter, which worked fine for string outputs such as name and status, but would not display the time field from my cursor. My time field in my data base is of type Timestamp. the query for the time field is
"strftime('%H:%M',time(sql_start_date,'unixepoch')) as start_time, " +
A little bit of research and it turns out that SimpleCursorAdapters only work for strings. I found this a little confusing as I would have thought that the field start_time was in the format of a string (after all I can view the result in the Log window by saying:
Log.i(TAG,start_time);
So my next attempt is to use my own custom 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;
}
}
The code fails on the line:
t.setText(st);
with a null pointer exception. The 'start_time_display' textview is not being found by the adapter.
The xml file with the field (list_element.xml) is as follows:
<?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"
an开发者_如何学Pythondroid: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>
Which, as you can see, clearly contains teh 'start_time_display' text view.
Hopefully I am missing something simple that someone with a bit more experience (I am very new to android and my Java is Tres rusty)
Thanks is advance
Kev
Sorry guys, dont waste your time on this.
the problem was a typo.. I referred to android_id in my xml file when it should have been android:id
thanks anyway
Kev
精彩评论