Android从数据库查询出来的数据放在ListView上

Android 码拜 9年前 (2015-11-02) 1621次浏览
//查询学生
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()),不知道用什么来保存从数据库查询出来的数据,怎么写下去?
解决方案:15分
为什么不写个student类呢,在里面加上id userName passWord 等字段,从数据库读数据出来的时候构造一个student类,然后list.add(student);
解决方案:5分
一般创建些实体类来。然后弄个List集合就可以了。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Android从数据库查询出来的数据放在ListView上
喜欢 (0)
[1034331897@qq.com]
分享 (0)