我有两个名为List
和Task
的表。
ListID
和NAME
TaskId
,TaskListId
,Name
,Notes
,Completed
和Hidden
有3个列表ID,使用Personal
Business
,History
和TabManager
的标签页
我需要的是,如果我从个人或商业中删除任务,则通过设置Hidden = 1来隐藏它。然后我想在我的历史记录标签中显示。如何实现这一目标。 如何在SQLite中为此编写查询。我需要一个类似下面的查询
Select * from Task where (TaskListID=1 or 2 // this is the part giving me problem as TaskListId decides which task belongs to which tab like 1=personal,2=business ) AND hidden==1;
以下是用于显示当前选项卡数据的方法refreshTaskList()。
public void refreshTaskList() {
// get task list for current tab from database
Context context = getActivity().getApplicationContext();
TaskListDB db = new TaskListDB(context);
//ArrayList<Task> tasks=new ArrayList<Task>();
if(Objects.equals(currentTabTag, "History"))
{
ArrayList<Task> tasks=db.getHistory(currentTabTag);
TaskListAdapter adapter = new TaskListAdapter(context, tasks);
taskListView.setAdapter(adapter);
}else
{
ArrayList<Task>tasks2=db.getTasks(currentTabTag);
TaskListAdapter adapter = new TaskListAdapter(context, tasks2);
taskListView.setAdapter(adapter);
}
以下是用于从当前Tab的数据库中提取任务的getTasks和getHistory方法。
public ArrayList<Task> getTasks(String listName) {
String where =
TASK_LIST_ID + "= ? AND " +
TASK_HIDDEN + "!='1'";
long listID = getList(listName).getId();
String[] whereArgs = {Long.toString(listID)};
this.openReadableDB();
Cursor cursor = db.query(TASK_TABLE, null,
where, whereArgs,
null, null, null);
ArrayList<Task> tasks = new ArrayList<Task>();
while (cursor.moveToNext()) {
tasks.add(getTaskFromCursor(cursor));
}
if (cursor != null)
cursor.close();
this.closeDB();
return tasks;
}
public ArrayList<Task> getHistory(String listName)
{
String where =
TASK_LIST_ID + "= ? AND " +
TASK_HIDDEN + "=='1'";
long listID = getList(listName).getId();
String[] whereArgs = {Long.toString(listID)};
this.openReadableDB();
Cursor cursor = db.query(TASK_TABLE, null,
where, whereArgs,
null, null, null);
Log.v(TAG,"Value of cursor is-" +cursor);
ArrayList<Task> tasks = new ArrayList<Task>();
while (cursor.moveToNext()) {
tasks.add(getTaskFromCursor(cursor));
}
if (cursor != null)
cursor.close();
this.closeDB();
return tasks;
}
我认为TASK_LIST_ID充当外键,决定哪个任务属于DB中的哪个选项卡。所以我需要的是一个这样的查询,我可以从两个标签中获取任务,即个人和商业,如果他们的隐藏值设置为1,则将它们放入历史记录中。
答案 0 :(得分:1)
如果1或2部分给你带来了困难,那么你来吧:
SELECT * FROM task WHERE TaskListID=1 OR TaskListID=2 AND HIDDEN=1