有没有办法读取sqlite表中的所有行并在textview中一次显示它们? 这就是我读它们的方式,它逐行阅读....
//---retrieves all the titles---
public Cursor getAllTitles()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_ISBN,
KEY_TITLE,
KEY_PUBLISHER},
null,
null,
null,
null,
null);
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;
public class DatabaseActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DBAdapter db = new DBAdapter(this);
//---get all titles---
db.open();
Cursor c = db.getAllTitles();
if (c.moveToFirst())
{
do {
DisplayTitle(c);
} while (c.moveToNext());
}
db.close();
}
}
public void DisplayTitle(Cursor c)
{
Toast.makeText(this,
"id: " + c.getString(0) + "\n" +
"ISBN: " + c.getString(1) + "\n" +
"TITLE: " + c.getString(2) + "\n" +
"PUBLISHER: " + c.getString(3),
Toast.LENGTH_LONG).show();
}
答案 0 :(得分:7)
首先,您可能需要查看列表视图以轻松显示此类数据列表。
如果您的目标确实是在一个文本视图中显示所有信息(或者正在制作吐司),您可以尝试制作一个大字符串,用它来创建吐司:
//---get all titles---
db.open();
Cursor c = db.getAllTitles();
String text = "";
if (c.moveToFirst())
{
do {
DisplayTitle(c, text);
} while (c.moveToNext());
}
db.close();
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
}
public void DisplayTitle(Cursor c, String text)
{
text +=
"id: " + c.getString(0) + "\n" +
"ISBN: " + c.getString(1) + "\n" +
"TITLE: " + c.getString(2) + "\n" +
"PUBLISHER: " + c.getString(3);
}
答案 1 :(得分:1)
db.getReadableDatabase();
StringBuffer sb=new StringBuffer();
Cursor c=db.rawQuery(SELECT * FROM TABLE_NAME);
while(c.moveToNext){
sb.append(c.getString(0);//c.getString(Column Index)
sb.append(c.getString(1);
//getString( till n number of Columns you have )
}
textView.setText(sb);