返回已完成和待处理任务的计数

时间:2016-04-20 07:50:50

标签: java android database sqlite

我在SQlite数据库中保存任务。这些任务具有状态。

任务表有一个状态列,其中0表示待处理任务,1表示已完成任务。

因此,为此,我创建了一个查询,以获取有关任务状态的已完成和待处理任务。

问题是我没有得到适当的任务数量。如果我有一个状态为待处理的任务,即0,那么它也会为两个查询返回1,即已完成和待处理。

我创建了一个任务助手并创建了两个不同的查询来获取任务数。后来我将这个计数添加到MPAndroidchart条目中。

编辑:任务表帮助:

public class TaskTableHelper扩展了SQLiteOpenHelper {

private static final String TASK_TABLE = "taskTable";
private static final String KEY_TASK_TITLE = "taskTitle";
private static final String KEY_TASK_ID = "taskId";
private static final String KEY_TASK_ALERT_DATE = "taskAlertDate";
private static final String KEY_TASK_ALERT_TIME = "taskAlertTime";
private static final String KEY_DUE_DATE = "dueDate";
private static final String KEY_DUE_TIME = "dueTime";
private static final String KEY_TASK_LIST ="taskList";
private static final String KEY_TASK_STATUS = "taskStatus";

private static final String KEY_TASK_PRIORITY = "taskPriority";

private static int count=0;

public TaskTableHelper(Context context) {
    super(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {
}


// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS " + TASK_TABLE);

    // createTable(db);
    // onCreate(db);
}

public void addTask(Task task) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(KEY_TASK_TITLE, task.getTitle());
    values.put(KEY_TASK_PRIORITY, task.getTaskPriority());
    values.put(KEY_TASK_ALERT_DATE, task.getAlertDate());
    values.put(KEY_TASK_ALERT_TIME, task.getAlertTime());
    values.put(KEY_DUE_TIME, task.getDueTime());
    values.put(KEY_DUE_DATE, task.getDueDate());
    values.put(KEY_TASK_LIST, task.getList());
    values.put(KEY_TASK_STATUS, task.getStatus());

    db.insert(TASK_TABLE, null, values);

    db.close();
}

public Task getTask(int id) {

    SQLiteDatabase db = this.getReadableDatabase();

    Task task = new Task();

    Cursor cursor = db.query(TASK_TABLE, new String[]{KEY_TASK_ID,
                    KEY_TASK_TITLE, KEY_TASK_PRIORITY, KEY_TASK_ALERT_DATE,KEY_TASK_ALERT_TIME, KEY_DUE_DATE, KEY_DUE_TIME,
                    KEY_TASK_LIST,KEY_TASK_STATUS}, KEY_TASK_ID + "=?",
            new String[]{String.valueOf(id)}, null, null, null, null);

    //cursor.moveToFirst();
    if( cursor != null && cursor.moveToFirst() ) {

        task = new Task(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1), Integer.parseInt(cursor.getString(2)),cursor.getString(3),
                cursor.getString(4), cursor.getString(5), cursor.getString(6),Integer.parseInt(cursor.getString(7)),
                Integer.parseInt(cursor.getString(8)));
    }

    return task;
}

public ArrayList<Task> getAllTask() {
    ArrayList<Task> conList = new ArrayList<Task>();

    String selectQuery = "SELECT  * FROM " + TASK_TABLE;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {

            Task task = new Task();

            task.setId(Integer.parseInt(cursor.getString(0)));
            task.setTitle(cursor.getString(1));
            task.setTaskPriority(Integer.parseInt(cursor.getString(2)));
            task.setAlertDate(cursor.getString(3));
            task.setAlertTime(cursor.getString(4));
            task.setDueDate(cursor.getString(5));

            task.setDueTime(cursor.getString(6));
            task.setList(Integer.parseInt(cursor.getString(7)));
            task.setStatus(Integer.parseInt(cursor.getString(8)));

            conList.add(task);
        } while (cursor.moveToNext());
    }

    return conList;
}


public ArrayList<Task> getAllTask(int listId) {
    ArrayList<Task> conList = new ArrayList<Task>();

    String selectQuery = "SELECT  * FROM " + TASK_TABLE + " WHERE " + KEY_TASK_LIST + " == " + listId;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {

            Task task = new Task();

            task.setId(Integer.parseInt(cursor.getString(0)));
            task.setTitle(cursor.getString(1));
            task.setTaskPriority(Integer.parseInt(cursor.getString(2)));
            task.setAlertDate(cursor.getString(3));
            task.setAlertTime(cursor.getString(4));
            task.setDueDate(cursor.getString(5));

            task.setDueTime(cursor.getString(6));
            task.setList(Integer.parseInt(cursor.getString(7)));
            task.setStatus(Integer.parseInt(cursor.getString(8)));

            conList.add(task);
        } while (cursor.moveToNext());
    }

    return conList;
}


public int getCompletedTasks() {
    ArrayList<Task> conList = new ArrayList<Task>();

    String selectQuery = "SELECT  * FROM " + TASK_TABLE + " WHERE " + KEY_TASK_STATUS + " = " + " 1 ";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor mCount= db.rawQuery(selectQuery,null);

    if (mCount.moveToFirst()) {
        do {
            Task task = new Task();
            count = mCount.getInt(0);
        } while (mCount.moveToNext());
    }

    Log.d("query",selectQuery);

    Log.d("count",String.valueOf(count));
    mCount.close();

    return count;
}

public int getPendingTasks() {
    ArrayList<Task> conList = new ArrayList<Task>();

    String selectQuery = "SELECT  * FROM " + TASK_TABLE + " WHERE " + KEY_TASK_STATUS + " = " + " 0 ";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor mCount= db.rawQuery(selectQuery,null);

    if (mCount.moveToFirst()) {
        do {
            Task task = new Task();
            count = mCount.getInt(0);
        } while (mCount.moveToNext());
    }

    Log.d("query",selectQuery);

    Log.d("count",String.valueOf(count));
    mCount.close();

    return count;
}

主要活动:

 private void setGraph() {

    mBarChart = (HorizontalBarChart) findViewById(R.id.chart);

    mBarChart.setScaleEnabled(false);
    ArrayList<BarDataSet> dataSets = null;

    ArrayList<Task> completedTasksList = new ArrayList<>();
    ArrayList<Task> pendingTasksList = new ArrayList<>();
    ArrayList<Integer> completedTask = new ArrayList<>();
    ArrayList<BarEntry> taskEntries = new ArrayList<>();

    int pendingTasks = mDb.getPendingTasks();
    int completedTasks = mDb.getCompletedTasks();


    taskEntries.add(new BarEntry(completedTasks,1));


        taskEntries.add(new BarEntry(pendingTasks,0));


    mBarChart.animateXY(2000, 2000);


    ArrayList<String> xAxis = new ArrayList<>();
    xAxis.add("Pending");
    xAxis.add("Completed");


    BarDataSet completed = new BarDataSet(taskEntries, "Entries");

   }

这里出了什么问题?

谢谢。

2 个答案:

答案 0 :(得分:2)

 count = mCount.getInt(0);

是错误的,因为你正在阅读原始女巫中的第一个字段可能是id字段。 使用此mCount.getCount();

答案 1 :(得分:0)

我建议使用编译语句,这是一种更快的计算方法:

    public long getCompletedTasks() {
        SQLiteDatabase db = getWritableDatabase();
        long count = -1;

        try {
            SQLiteStatement statement = db.compileStatement(
                "select count(*) from table_name where status = 1");
            count = statement.simpleQueryForLong();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (statement != null) {
                statement.close();
            }
        }

        return count;
    }

另外,如果你知道你经常使用这个方法,你可以缓存语句对象并重用它。