Android:在显示前对Cursor中的SQL返回值执行算术运算
问题描述:
我在SQLiteDatabase中存储了一个通话时间(以秒为单位),我需要以分钟形式显示给用户。这里是语句:Android:在显示前对Cursor中的SQL返回值执行算术运算
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(四舍五入最近的分钟)。我该怎么做呢?
答
你可以在适配器上使用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;
}
});
答
在选择陈述式的,而不是做SELECT * - 指定列 - 你可以这样做: 选择column_phone,列名,(column_duration/60)+ 1从等等等等column_duration
这完全做的工作。谢谢。 – moonlightcheese 2011-03-15 21:21:57