我想从我的 sqlite 数据库中查询所有行,然后将其传递给列表视图以显示在我的应用程序上。
这是代码(我确实有一个 student.java 实现):
StudentDAO.java:
public void add(Student student)
{
db = helper.getWritableDatabase();
db.execSQL("insert into t_student (sid, name, age) values (?,?,?)", new Object[]
{ student.getId(), student.getName(), student.getAge()}
);
}
public Student findAll()
{
db = helper.getWritableDatabase();
Cursor cursor = db.rawQuery("select sid, name, age from t_student", (String[]) new Object()
);
if(cursor.moveToNext())
return new Student(cursor.getInt(cursor.getColumnIndex("sid")), cursor.getString(cursor.getColumnIndex("name")), cursor.getShort(cursor.getColumnIndex("age")));
return null;
}
MultiList.java:
public class MultiList extends ListActivity {
ListView lv;
SimpleAdapter sd;
StudentDAO dao = new StudentDAO(MultiList.this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Student Alex = new Student();
chan.setId(1);
chan.setAge((short) 18);
chan.setName("Alex ");
dao.add(Alex );
Student Queena = new Student();
chan2.setId(2);
chan2.setAge((short) 19);
chan2.setName("Queena");
dao.add(Queena);
Student Tom = new Student();
chan3.setId(3);
chan3.setAge((short) 20);
chan3.setName("Tom");
dao.add(Tom);
dao.findAll();////how to store them???
如您所见,现在,在我将所有三个学生对象都插入到我的数据库中之后,我如何读取并存储它们?因为我的 findAll() 方法返回一个学生对象。抱歉,这可能是一个简单的问题,请帮助