从数据库查询出来的数据放在ListView上 安卓解决办法
从数据库查询出来的数据放在ListView上 安卓
//查询学生
public Student queryStudent(){
SQLiteDatabase db=null;
Cursor cursor=null;
List<Student> list;
try{
db=studentHelper.getWritableDatabase();
String sql="select * from student";
cursor=db.rawQuery(sql, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String passWord = cursor.getString(cursor.getColumnIndex("pwd"));
//list.add();
}
}catch(Exception e){
e.printStackTrace();
}finally{
db.close();
}
return null;
}
另外一个类中组件:
tvId=(TextView) findViewById(R.id.tv_StudentId);
tvName=(TextView) findViewById(R.id.tv_StudentName);
tvPwd=(TextView) findViewById(R.id.tv_Studentpwd);
//tvId.setText();
跪求大神!
1、第一个红色区(list.add()),不知道用什么来保存从数据库查询出来的数据,不知道怎么写下去?
------解决思路----------------------
为什么不写个student类呢,在里面加上id userName passWord 等字段,从数据库读数据出来的时候构造一个student类,然后list.add(student);
------解决思路----------------------
一般创建些实体类来。然后弄个List集合就可以了。
//查询学生
public Student queryStudent(){
SQLiteDatabase db=null;
Cursor cursor=null;
List<Student> list;
try{
db=studentHelper.getWritableDatabase();
String sql="select * from student";
cursor=db.rawQuery(sql, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String passWord = cursor.getString(cursor.getColumnIndex("pwd"));
//list.add();
}
}catch(Exception e){
e.printStackTrace();
}finally{
db.close();
}
return null;
}
另外一个类中组件:
tvId=(TextView) findViewById(R.id.tv_StudentId);
tvName=(TextView) findViewById(R.id.tv_StudentName);
tvPwd=(TextView) findViewById(R.id.tv_Studentpwd);
//tvId.setText();
跪求大神!
1、第一个红色区(list.add()),不知道用什么来保存从数据库查询出来的数据,不知道怎么写下去?
------解决思路----------------------
为什么不写个student类呢,在里面加上id userName passWord 等字段,从数据库读数据出来的时候构造一个student类,然后list.add(student);
------解决思路----------------------
一般创建些实体类来。然后弄个List集合就可以了。