編輯:Android 開發實例
前面幾篇已經把每個月的收支明細寫進 SQLite 資料表了,這一篇要解決的是反過來的問題:怎麼把資料從資料表裡撈出來,攤成一個看得懂的帳單明細畫面。

這個版面我其實猶豫過好幾個方案。原本想用 GridView,光看名字就覺得很像我要的東西,但查了資料、也實測了一下,發現跟想像落差不小,最後還是回到 ListView。成品結構其實很單純:上面一條表頭(LinearLayout)、中間一個 ListView、下面一條 footer(LinearLayout)顯示當月合計。
要把資料塞進 ListView,核心觀念就是 Adapter。當資料來自資料庫時,SimpleCursorAdapter 是最省事的选择,它的建構子參數一長串,但實際上你只要準備兩樣東西:一個 Cursor 和 一個列樣式。
SimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to)
用法如下:
String[] from = new String[] {"rowid", "name", "fee", "sdate", "desc"};
int[] to = new int[] { R.id.item1, R.id.item2, R.id.item3, R.id.item4, R.id.item5 };
SimpleCursorAdapter mAdapter =
new SimpleCursorAdapter(this, R.layout.grid_items, cur, from, to);
lv.setAdapter(mAdapter);
from 是「要從 Cursor 取哪些欄位」,to 是「分別塞進哪個 TextView」,兩個陣列的長度與順序必須一一對應。本例的 Cursor 帶有 rowid、name、fee、sdate、desc 五個欄位。
而 R.layout.grid_items 就是每一列的樣式,橫向排五個 TextView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/item1"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:width="20dip" />
<TextView android:id="@+id/item2"
android:layout_height="fill_parent"
android:text="賬目"
android:width="60dip"
android:layout_width="wrap_content" />
<TextView android:id="@+id/item3"
android:text="費用(元)"
android:textSize="14dip"
android:textStyle="bold|italic"
android:width="60dip"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<TextView android:id="@+id/item4"
android:layout_height="fill_parent"
android:text="日期"
android:width="80dip"
android:layout_width="wrap_content" />
<TextView android:id="@+id/item5"
android:layout_height="fill_parent"
android:text="備註"
android:width="100dip"
android:layout_width="wrap_content" />
</LinearLayout>
在 Adapter 的 to 參數中指定這些 TextView 各使用哪個 Cursor 欄位的值,然後 lv.setAdapter(mAdapter) 完成綁定,就這樣。
package com.cola.ui;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class Grid_bills extends Activity {
BilldbHelper billdb;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setTitle("ColaBox-帳單明細(2008-11月)");
setContentView(R.layout.grid_bills);
billdb = new BilldbHelper(this);
Cursor cur = billdb.getBills();
ListView lv = (ListView) findViewById(R.id.listview);
String[] from = new String[] {"rowid", "name", "fee", "sdate", "desc"};
int[] to = new int[] { R.id.item1, R.id.item2, R.id.item3, R.id.item4, R.id.item5 };
SimpleCursorAdapter mAdapter =
new SimpleCursorAdapter(this, R.layout.grid_items, cur, from, to);
lv.setAdapter(mAdapter);
// 底部合計
TextView total = (TextView) findViewById(R.id.totalitem);
total.setText(billdb.getBillsTotal("2008-11"));
}
}
整頁的版面配置(grid_items.xml)則是「表頭 + ListView + footer」三段包在一個 ScrollView 裡:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout android:id="@+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout android:id="@+id/layouthead"
android:background="#ffCded8b"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<!-- 五個表頭 TextView:item1 ~ item5 -->
</LinearLayout>
<View android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider" />
<LinearLayout android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:minHeight="372dip">
<ListView android:id="@+id/listview"
android:layout_height="fill_parent"
android:layout_width="fill_parent"></ListView>
</LinearLayout>
<LinearLayout android:id="@+id/layoutfoot"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffCded8b">
<TextView android:id="@+id/totalitem"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</ScrollView>
這裡遇到一個不算冷門但很惱人的問題。我在資料庫裡的 fee 欄位是 integer,單位是「分」,也就是 10000 代表 100 元。用 select fee/100 from bills 查詢時,SQLite 做的是整數除法,小數直接被切掉,畫面上只剩整數。
後來我試著用字串串接的方式硬湊:select fee/100||'' from bills,小數是出來了,但接著又發現 999999.99 / 100 顯示成 1000000。同樣的查詢在 adb shell 裡跑是 999999.99,一進到 ListView 就變成 1000000,合理懷疑是 Adapter 取值時走了 getString() 的路徑造成的。
與其跟 SQLite 的型別轉換纏鬥,更可靠的做法是在 Java 層自己格式化。既然底層存的是「分」,那就自己還原:
// fee 為以「分」為單位的整數
int fee = 12345;
String text = (fee / 100) + "." + String.format("%02d", fee % 100); // 123.45
如果堅持用 Cursor 直接顯示,那就在 from 裡放一個自訂運算式欄位,並明確指定它的型別;但實務上把格式化邏輯放在程式碼裡,可讀性與可控性都好得多,也不必依賴 SQLite 的隱含轉換規則。這個教訓很值得記住:金額一律用整數存最小單位,顯示時再轉,能避開絕大多數精度與進位的麻煩。
Android 登錄頁面實作:表單驗證與錯誤次數控制
登錄頁面的基本需求 登錄畫面通常包含使用者名稱、密碼與登錄按鈕。為了避免暴力嘗試,還可以加入錯誤次數限制。當次數用完時,停用登錄按鈕或提示使用者稍後再試。 建立輸入欄位
Android 中的 Handler 與多執行緒:一個查詢小程式的完整實作
先說清楚 Handler 是拿來做什麼的,再用一個實際例子看它在多執行緒裡怎麼運作。 Handler 到底是什麼 通俗地說,Handler 是用來在不同執行緒之間傳遞資
Android 的 windowSoftInputMode 屬性詳解
在前面的一篇文章中,簡單介紹了如何實現軟鍵盤不自動彈出,使用的方法是設定 android:windowSoftInputMode 屬
Android 音訊播放實作:MediaPlayer 與播放控制
MediaPlayer 可用來播放 raw 或指定來源的音訊。建立物件後,即可控制播放、暫停與跳轉。 MediaPlayer mediaPlayer = MediaPla