gpt4 book ai didi

android - 如何将数据放入列中,数据从 sqlite 中的一行获取并放入字符串/ TextView

转载 作者:行者123 更新时间:2023-11-29 15:24:02 25 4
gpt4 key购买 nike

如何将数据一行从 sqlite 到 textview。我有 2 个 textview,我想从数据库 sqlite 中获取数据。这是我的查询:

public Cursor getItem(long itemId) {
SQLiteDatabase db = this.getWritableDatabase();
String selectQuery = "SELECT name, link FROM tes WHERE "
+ BaseColumns._ID + "=" + itemId;
Cursor cursor = db.rawQuery(selectQuery, null);
return cursor;
}

以及如何将名称和链接的内容放入 TextView 。我想在下面输入:

String name = ""; // how to get contents of name from coloumn name in my query?
String link = ""; // how to get contents of link from coloumn link in my query?

textName.setText(name);
textLink.setText(link);

谢谢。

最佳答案

试试这个:

Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();

if (cursor.getCount() > 0) {
String name = cursor .getString(cursor.getColumnIndex("name")).trim();
String link = cursor .getString(cursor.getColumnIndex("link ")).trim();

textName.setText(name);
textLink.setText(link);

}
cursor.close();

关于android - 如何将数据放入列中,数据从 sqlite 中的一行获取并放入字符串/ TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14808589/

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