Android中列表视图的自定义光标适配器问题

时间:2010-08-10 11:44:01

标签: java android listview cursor

我有一个应用程序,它将查询数据库并尝试以列表格式输出结果。

我的第一次尝试是使用simpleCursorAdapter,它对字符串输出(如名称和状态)工作正常,但不会显示光标的时间字段。我的数据库中的时间字段是Timestamp类型。时间字段的查询是

"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是Tres生锈的)

谢谢是提前

千电子伏

1 个答案:

答案 0 :(得分:0)

对不起,伙计们,不要浪费你的时间。

问题是一个错误..我在我的xml文件中提到android_id应该是android:id

非常感谢

千电子伏