我正在尝试使用 SQLite 数据库和 Drag-Sort ListView 创建一个简单的待办事项列表应用程序.
现在,我正在使用 SimpleCursorAdapter 将 SQLite 数据库游标中的数据绑定(bind)到 ListView 中,并使用 EditText View 添加项目,如本 tutorial 中所述。 .
我已将所有内容移至一个事件中,并将普通 ListView 替换为拖动排序 ListView。我的问题是将 DB 和 Drag-Sort ListView 连接在一起。 DLSV 演示使用预定义的数组来填充 DSLV。
代码段形式 DSLV:
DragSortListView lv = (DragSortListView) getListView();
lv.setDropListener(onDrop);
lv.setRemoveListener(onRemove);
array = getResources().getStringArray(R.array.jazz_artist_names);
list = new ArrayList<String>(Arrays.asList(array));
adapter = new ArrayAdapter<String>(this, R.layout.list_item1, R.id.text1, list);
setListAdapter(adapter);
我现有代码的片段:
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
...
mDbHelper.createNote(noteName, "");
fillData();
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = mDbHelper.fetchAllNotes();
startManagingCursor(c);
String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
int[] to = new int[] { R.id.text1 };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
setListAdapter(notes);
}
谢谢您,如果这没有多大意义,我们深表歉意。
最佳答案
由于您无法对 Cursor 中的项目重新排序,因此您必须在 Cursor 位置和 ListView 位置之间创建一个映射。这是由 DragSortCursorAdapter 为您完成的类和子类,以便在视觉上保持拖放重新排序。如果您需要持久保存新订单(比如保存到您的数据库),那么您必须自己实现该逻辑。方法getCursorPositions()将帮助您解决这个问题。
关于drag-and-drop - 在 SQLite 数据库中使用拖放排序 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12472888/