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

时间:2011-12-09 06:32:57

标签: android

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

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());
        } 
    }
}

2 个答案:

答案 0 :(得分:0)

试试这种方式

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");

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

答案 1 :(得分:0)

希望这会奏效。

 String data = "<html>" +
    "<body><table border=2> ";
if  (c.moveToFirst()) {

      do {

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

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

      data  += "<tr> <td>"+firstName+"</td>+
     <td>"+lastName+"</td></tr>";

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