2010-08-11 100 views
0

我有一個問題,通過SimpleCursorAdapter將sql查詢的結果顯示爲ListView。這是我的查詢:Android SimpleCursorAdapter/ListView問題

String sql = "" + 
"SELECT (Clients.firstname || \" \" || Clients.surname) AS client_name, " + 
    "Visits._id, " + 
    "Status.status, " + 
"strftime('%H:%M',time(sql_start_date,'unixepoch')) as start_time, " + 
"strftime('%H:%M',time(sql_end_date,'unixepoch')) as end_time " + 
"FROM " + 
     "Visits,Clients,Status " + 
"WHERE " + 
     "Visits.status = Status.remote_id " + 
"AND " + 
     "Visits.client_id = Clients.remote_id " + 
"AND " + 
    "Visits.sql_start_date > "+checkpoint+" " + 
"AND " + 
    "Visits.sql_end_date < "+endpoint; 

當我執行此查詢,我得到一個典型的結果集,像這樣:

client_name |Visit._id|Status.status |start_time |end_time 
__________________________________________________________ 
Kevin Bradshaw|187  |Pending  |13:00  |14:00 
Peter Flynn |193  |Pending  |15:00  |16:30 

我想這個輸出光標到ListView。 我遇到的問題是,我可以輸出客戶端名稱和狀態沒有問題,但start_time和end_time字段保持空白。

我的光標適配器代碼是這樣的:

Cursor cur = HomeScreen.this.application.getVisitsHelper().fetchVisits(); 
startManagingCursor(cur); 
// the desired columns to be bound  
String[] columns = new String[] {VisitsAdapter.KEY_CLIENT_FULL_NAME, VisitsAdapter.KEY_STATUS,VisitsAdapter.KEY_CLIENT_START_TIME, VisitsAdapter.KEY_CLIENT_END_TIME}; 

// the XML defined views which the data will be bound to 
int[] to = new int[] { R.id.name_entry,R.id.number_entry,R.id.start_time_display,R.id.end_time_display }; 

// create the adapter using the cursor pointing to the desired data as well as the layout information 
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(HomeScreen.this, R.layout.list_element, cur, columns, to); 

// set this adapter as ListActivity's adapter 
HomeScreen.this.setListAdapter(mAdapter); 

最後我的XML佈局(list_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> 

所以我的問題是,爲什麼一些代碼(即狀態和客戶端名稱)獲取輸出,而我的時間字段(start_time,end_time)不是?

是因爲簡單的遊標適配器只能處理整數和字符串,而我的時間變量來自數據庫中的SQL時間戳?

回答

1

對不起,夥計們,不要浪費你的時間在這個。

問題是一個錯字。我提到我的XML文件android_id時,它應該是機器人:ID

感謝反正

千電子伏

+0

不要忘了接受你自己的答案! – Josh 2010-08-11 14:04:56

+0

@josh,你不能直接接受你自己的答案,當我早些時候試圖這樣做時,它告訴我必須等待22個小時才能接受我自己的答案 – 2010-08-11 18:15:03