gpt4 book ai didi

android - 如何显示数据库中的所有行

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

在下面的代码中,我只得到数据库中的最后一行,即数据库中最后一行的名字和姓氏。我的问题是:如何更改此代码才能打印所有结果?

package samples.directory;      

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class clanguage extends Activity {
protected Cursor c;
protected SQLiteDatabase db;


protected TextView employeeName;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webdesign);
WebView engine = (WebView) findViewById(R.id.web_engine);

db = (new DatabaseHelper(this)).getWritableDatabase();

Cursor c = db.rawQuery("SELECT firstName, lastName FROM employee", null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String firstName= c.getString(c.getColumnIndex("firstName"));
String lastName = c.getString(c.getColumnIndex("lastName "));
String data = "<html>" +
"<body><table border=2> <tr> <td>"+firstName+"</td>+
<td>"+lastName+"</td></tr></body></html>";

engine.loadData(data, "text/html", "UTF-8");

}while (c.moveToNext());
}
}
}

最佳答案

试试这个

StringBuffer data = new StringBuffer("<html>");
data.append("<body><table border=2>");
do {

String firstName= c.getString(c.getColumnIndex("firstName"));
String lastName = c.getString(c.getColumnIndex("lastName "));

data.append("<tr> <td>"+firstName+"</td><td>"+lastName+"</td></tr>");

}while (c.moveToNext());
data.append("</table></body></html>");
engine.loadData(data, "text/html", "UTF-8");

您每次都覆盖数据对象的内容并加载它。

关于android - 如何显示数据库中的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8441921/

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