我的问题是 TextView 中显示的唯一数据是表格的最后一整行。
所以这是我从数据库表中获取全部数据的代码:
public List<Person> getAllPerson()
{
List<Person> personList = new ArrayList<Person>();
//select query
String selectQuery = "SELECT * FROM " + DATABASE_TABLE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
{
Person person = new Person();
person.setId(Integer.parseInt(cursor.getString(0)));
person.setName(cursor.getString(1));
person.setHotness(cursor.getString(2));
person.setAge(Integer.parseInt(cursor.getString(3)));
// Adding person to list
personList.add(person);
}
return personList;
}
这是我将表格数据显示到 TextView 中的代码:
public class SQLiteView extends Activity
{
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlview);
tv = (TextView) findViewById(R.id.tvDBdisplay);
DbHelper d = new DbHelper(this);
List<Person> person = d.getAllPerson();
for (Person p: person)
{
String data =p.getId() + " " + p.getName() + " " + p.getHotness() + " " + p.getAge();
tv.setText(data);
}
}
}