gpt4 book ai didi

java - 访问 SQLite 数据库的行

转载 作者:行者123 更新时间:2023-12-02 04:27:33 24 4
gpt4 key购买 nike

我正在为数据库的一些代码而苦苦挣扎,我已经设法访问我单击的项目的 ID 号,但是我无法访问更多数据?我该怎么做?

例如,现在我有我的 ID 号,我想在单独的窗口中显示该行上的所有数据,但我似乎无法用光标提取数据

String itemselect = String.valueOf(spinner.getSelectedItem());
Toast.makeText(Developer.this, itemselect, Toast.LENGTH_LONG).show();
final Cursor cursor = mydb.getAllRows(itemselect);
startManagingCursor(cursor);

String[] fromfieldnames = new String[]{
DatabaseHelper.COL_1, DatabaseHelper.COL_2, DatabaseHelper.COL_3,
DatabaseHelper.COL_4, DatabaseHelper.COL_5, DatabaseHelper.COL_6 };
int[] toviewids = new int[]{R.id.textone, R.id.texttwo, R.id.textthree, R.id.textfour, R.id.textfive, R.id.textsix};
final SimpleCursorAdapter mycursoradapter = new SimpleCursorAdapter(this, R.layout.listtext, cursor, fromfieldnames, toviewids);
listView.setAdapter(mycursoradapter);

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Long string = mycursoradapter.getItemId(position);
Toast.makeText(Developer.this, string.toString(),Toast.LENGTH_LONG).show();
return true;
}
});

这段代码没有错误,我只需要尝试添加这个额外的功能

提前感谢M.

最佳答案

数据存储在 CursoronItemLongClick 中的行位置 position 处。在 cursor 上使用 moveToPosition() 方法,并检索值:

String one, two, three;

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
cursor.moveToPosition(position);
one = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_1));
two = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_2));
// Etc...
}
};

关于java - 访问 SQLite 数据库的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31941558/

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