开发者

Android: perform arithmetic on SQL return values in Cursor before displaying

i'm storing a call duration (in seconds) in a SQLiteDatabase and i need to display this as minutes to the user. here is the statement:

Cursor listCur = mDb.rawQuery(
    "SELECT * FROM " + mDbCallTable.TABLE_NAME + 
    " JOIN " + mDbPhoneTable.TABLE_NAME + 
    " ON " + mDbCallTable.TABLE_NAME+"."+mDbCallTable.COLUMN_PHONE+"="+mDbPhoneTable.TABLE_NAME+"."+mDbPhoneTable.COLUMN_PHONE +
    " JOIN " + mDbCustomerTable.TABLE_NAME + 
    " ON " + mDbPhoneTable.TABLE_NAME+"."+mDbPhoneTable.COLUMN_CUST_开发者_C百科ID+"="+mDbCustomerTable.TABLE_NAME+"."+mDbCustomerTable._ID,
    null
);

and here is where i connect it to the SimpleCursorAdapter for display:

ListView theList;
SimpleCursorAdapter adapter;
theList = (ListView)findViewById(R.id.call_list);
listCur.moveToFirst();
adapter = new SimpleCursorAdapter(
    MainActivity.this,
    R.layout.call_list_item,
    listCur,
    new String[] {
        mDbCallTable.TABLE_NAME+"."+mDbCallTable.COLUMN_PHONE, 
        mDbCustomerTable.TABLE_NAME+"."+mDbCustomerTable.COLUMN_NAME,
        mDbCallTable.TABLE_NAME+"."+mDbCallTable.COLUMN_DURATION 
    },
    new int[] {
        R.id.item_text_phone,
        R.id.item_text_customer,
        R.id.item_text_duration
    }
);
theList.setAdapter(adapter);

somewhere in here i need to divide by 60 to get the minutes and add 1 (rounding up to the nearest minute). how do i do this?


You can use a ViewBinder on the Adapter.

Something like

adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        if(columnIndex != 2) return false; // only care about 3rd column
        int duration = cursor.getInt(columnIndex) / 60 + 1;
        ((TextView)view).setText(String.valueOf(duration));
        return true;
    }
});


In the select statement- instead of doing select * - specify the columns - and you can do something like: select column_phone, column_name, (column_duration/60) + 1 as column_duration from blah blah

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜