java android sqlite一次读取所有行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6399288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android sqlite read all rows at once
提问by moe
Is there a way to read all the rows in an sqlite table and display them at once in a textview? This is how I read them and it reads line by line ....
有没有办法读取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();
}
回答by Jolanda Verhoef
First of all, you might want to look into listviews to easily display a list of data like this.
首先,您可能希望查看列表视图以轻松显示这样的数据列表。
If your goal really is to display all information in one textview (or toast as you're making now), you could try making one large string, with which you create the toast:
如果您的目标确实是在一个文本视图中显示所有信息(或您现在正在制作的吐司),您可以尝试制作一个大字符串,用它来创建吐司:
//---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);
}
回答by Pravinraj Venkatachalam
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);