【发布时间】:2016-11-24 10:03:10
【问题描述】:
在我的应用程序中,它有两个选项卡,一个是提醒,另一个是已完成任务。
单击切换按钮时,我希望它将列表移动到已完成的任务。
想法是:
- 从 sqlite 获取选中的行 id
- 根据 id 从提醒表中检索数据并插入到已完成表中。
- 在 Completed 选项卡中调用 Retrieve 方法。
但是当我点击切换按钮并滑动到已完成时,它仍然是空的。退出应用程序并滑动到选项卡后,仅显示数据。
当滑动而不是退出应用程序并重新打开时,如何使数据立即显示在已完成选项卡中?谢谢
AllAdapter(提醒)
holder.toggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (((ToggleButton)v).isChecked()) {
int getPosition = (Integer) v.getTag(); // Here we get the position that we have set for the checkbox using setTag.
search.get(getPosition).setSelected(((ToggleButton) v).isChecked());
int id= search.get(getPosition).getID();
mdb = new MyDatabaseHelper(v.getContext());
database = mdb.getReadableDatabase();
Cursor cursor = database.rawQuery("SELECT * FROM " + MyDatabaseHelper.TABLE__TASK + " WHERE ID = ? ", new String[]{id+""}, null);
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String allTask = cursor.getString(cursor.getColumnIndex("Title"));
String name = cursor.getString(cursor.getColumnIndex("Name"));
String allTime = cursor.getString(cursor.getColumnIndex("Time"));
String allDate = cursor.getString(cursor.getColumnIndex("Date"));
insertDataToCompleteTab(id,name,allTask,allTime,allDate); // insert to another table
}
}
} else {
int getPosition = (Integer) v.getTag(); // Here we get the position that we have set for the checkbox using setTag.
search.get(getPosition).setSelected(((ToggleButton) v).isChecked());
}
}
});
已完成任务
retrieveList(name);
public void retrieveList(String name) {
Toast.makeText(getActivity(),name,Toast.LENGTH_SHORT).show();
search.clear();
database = mdb.getReadableDatabase();
Cursor cursor = database.rawQuery("SELECT * FROM " + MyDatabaseHelper.TABLE_TASKCOMPLETED + " WHERE Name = ? ", new String[]{name}, null);
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
int iD = cursor.getInt(cursor.getColumnIndex("ID"));
String allTask = cursor.getString(cursor.getColumnIndex("Title"));
String allTime = cursor.getString(cursor.getColumnIndex("Time"));
String allDate = cursor.getString(cursor.getColumnIndex("Date"));
if (adapter != null) {
adapter.add(iD, allTask, allTime, allDate);
listview.setAdapter(adapter);
adapter.getCount();
// check();
}
}
} else {
}
}
AllAdapter
已完成标签
完整适配器
【问题讨论】:
-
如果您使用列表来显示数据,请确保在列表的适配器上调用 notifyDataSetChanged(),以便它使用新数据更新视图。
-
不要将数据从一个表移动到另一个表,而是只创建一个任务表,其中有一个布尔列,表示是否已完成。而切换只是从表中更新该字段。你的代码在哪里?
-
@AmrutBidri 请检查
-
@VeselinTodorov 请检查。
-
你能告诉我们你的适配器代码吗?另外,您将数据数组/列表保存在哪里。您从光标获得的更改应该反映在适配器使用的数组/列表中,然后调用 listview.getAdapter().notifyDataSetChanged()。无需每次都设置新的适配器。
标签: android sqlite listview swipe