在Android中显示数据库表数据
我正在尝试从 SQLite 数据库获取数据并在 Android 中以表格格式显示。下面是显示数据的代码:
Cursor c=db.getAllTitles();
if(c.moveToFirst())
{
do
{
DisplayTitle(c);
}while(c.moveToNext());
}
DisplayTitle
函数如下所示:
public void DisplayTitle(Cursor c)
{
txt.setText(c.getString(0));
txt.setTextColor(Color.blue(1));
tr.addView(txt);
tl.addView(tr);
}
这里,txt
是我全局声明的一个 TextView
变量在课堂上。 tl
是 TableLayout
变量,tr
是我在类中全局声明的 TableRow
变量。
这是行不通的。但是,如果我使用 Toast
函数,则会显示从数据库中提取的数据。请帮忙。
以下是整个代码的链接:
I am trying to get the data from SQLite database and display it in a table format in Android. Here is the code for displaying data:
Cursor c=db.getAllTitles();
if(c.moveToFirst())
{
do
{
DisplayTitle(c);
}while(c.moveToNext());
}
And the DisplayTitle
function looks as shown below:
public void DisplayTitle(Cursor c)
{
txt.setText(c.getString(0));
txt.setTextColor(Color.blue(1));
tr.addView(txt);
tl.addView(tr);
}
Here, txt
is a TextView
variable that I have globally declared in the class. tl
is the TableLayout
variable and tr
is TableRow
variable that I have declared globally in the class.
This doesn't work. However, if I use Toast
function, the data pulled from the database is getting displayed. Please help.
Here is the link to the entire code:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是,对于每个数据记录,您需要声明一个带有新文本视图的新表行。从解释来看,您似乎不想对所有数据记录使用相同的表行和相同的文本视图。
如果您仍然遇到困难,请发布更完整的代码示例。最好是完整的,以便其他人可以复制您的问题。另外,请解释一下“这不起作用”是什么意思。
My guess is that for each data record you need to declare a new table row with a new text view. From the explanation, it doesn't seem that you want to use the same table row and same text view for all data records.
If you're still stuck, please post a more complete code example. Preferably so complete that others can replicate your problem. Also, please explain what "This doesnt work" means.
尝试这个代码...
之后在主要活动中使用这个代码
try this code...
after that use this code in main activity