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

发布于 2024-09-13 20:30:16 字数 3335 浏览 3 评论 0原文

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

我的第一次尝试是使用 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>

如您所见,其中显然包含 teh。 'start_time_display' 文本视图。

希望我错过了一些简单的东西,有更多经验的人(我对 android 很陌生,我的 Java 很生锈)

谢谢凯夫

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"
  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>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

数理化全能战士 2024-09-20 20:30:16

对不起,伙计们,不要在这上面浪费时间。

问题是一个拼写错误..我在我的 xml 文件中引用了 android_id,而它应该是 android:id

无论如何,谢谢

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文