I want to query all rows from my sqlite database and then pass it to a listview to display on my app.
Here is the code(I do have a student.java implemented):
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???
As you can see, now, after i inserted all three students object into my database, how do i read them and store them? Because my findAll() method return a student object.
Sorry this might be a easy question, please help
There is two easy options, the best in my view is to use a CursorAdapter and pass a cursor with your query, in this case listView will load only showed elements and will dinamicaly load from database while you scroll.
Or you can execute the query for your self and store objects in an array to show with a SimpleCursorAdapter.