gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:41:58 27 4
gpt4 key购买 nike

我正在将任务保存在 SQlite 数据库中。这些任务有一个状态。

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

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

问题是我没有得到正确的任务计数。如果我有一个任务的状态为挂起,即 0,那么它也会为两个查询返回 1,即已完成和挂起。

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

编辑:任务表助手:

公共(public)类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;
}

主要 Activity :

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

}

这里出了什么问题?

谢谢。

最佳答案

 count = mCount.getInt(0);

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

关于java - 返回已完成和待处理任务的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36737210/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com