从sqlite字符串列中获取总数

时间:2013-01-03 00:38:39

标签: android sqlite

如何添加“swimm_lap”列的总数(存储为字符串的数字)并在textview中显示。 我尝试添加类似... select sum(swimm_lap)....来自

这是我在适配器中的内容

public Cursor Query(String filter, String date_filter, String lap_filter) {
    if (date_filter == null & lap_filter == null){
            int sum = 0;
            Cursor cursor = database.rawQuery
            ("select sum(swimm_lap) _id,swimm_pos,swimm_date, swimm_lap,swimm_stroke,swimm_time,swimm_media,swimm_efficiency,swimm_note from swimm_table order by cast("+filter+" as integer) asc", null);          
            if (cursor.moveToFirst()) 
            {sum = cursor.getInt(0);}
            return cursor;}

并在主要活动中

totallap = (TextView) findViewById(R.id.total_lap);
totallap.setText(String.valueOf(sum));
public void Bindadapter() {   
        datasource.open();
    Cursor cursor = datasource.Query(filter, date_filter, lap_filter);

    String[] columns = new String[] { "swimm_pos", "swimm_date","swimm_lap","swimm_stroke", "swimm_time", "swimm_media", "swimm_efficiency", "swimm_note" };
    int[] to = new int[] { R.id.swimm_pos, R.id.swimm_date, R.id.swimm_lap, R.id.swimm_stroke, R.id.swimm_time, R.id.swimm_medialap, R.id.swimm_efficiency, R.id.swimm_note};

SimpleCursorAdapter adapter = new SimpleCursorAdapter(
        this, 
        R.layout.list_layout, 
        cursor, 
        columns, 
        to);


adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
     if (view.getId() == R.id.total_lap)
        { 
        sum = cursor.getInt(0);
        String s = String.valueOf(sum);
        TextView tv = (TextView)view;
        tv.setText(s);
        return true;
        }

1 个答案:

答案 0 :(得分:0)

int sum = 0;
Cursor cursor = database.rawQuery("SELECT SUM(swimm_lap) FROM swimm_table", null);

if (cursor.moveToFirst()) 
{
    sum = cursor.getInt(0);
}