Android:在显示之前对Cursor中的SQL返回值执行算术运算

时间:2011-03-15 19:13:18

标签: android sql sqlite listview cursor

我在SQLiteDatabase中存储一个呼叫持续时间(以秒为单位),我需要将其显示为用户的分钟。这是声明:

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_ID+"="+mDbCustomerTable.TABLE_NAME+"."+mDbCustomerTable._ID,
    null
);

这里是我将它连接到SimpleCursorAdapter进行显示的地方:

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);

在这里的某个地方,我需要除以60得到分钟并加1(向上舍入到最近的分钟)。我该怎么做?

2 个答案:

答案 0 :(得分:3)

您可以在适配器上使用ViewBinder

这样的东西
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;
    }
});

答案 1 :(得分:1)

在select语句中 - 而不是执行select * - 指定列 - 您可以执行以下操作: 从blah blah选择column_phone,column_name,(column_duration / 60)+ 1作为column_duration