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

day_moon 2015-11-01 06:18:49
//查询学生
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()),不知道用什么来保存从数据库查询出来的数据,不知道怎么写下去?
...全文
643 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dai_jiawei 2015-11-02
  • 打赏
  • 举报
回复
一般创建些实体类来。然后弄个List集合就可以了。
lyyzms 2015-11-02
  • 打赏
  • 举报
回复
你把实体类传到另外一个Activity里不就可以了吗
day_moon 2015-11-02
  • 打赏
  • 举报
回复
while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("id")); String name = cursor.getString(cursor.getColumnIndex("name")); String passWord = cursor.getString(cursor.getColumnIndex("pwd")); //把从数据库取出来的数据放到一个实体类里面 student.setId(id); student.setName(name); student.setPwd(passWord); //把实体类添加到集合里面 list.add(student); 哦。在第二个红色的区域,怎么把这些数据取出来(//把从数据库取出来的数据放到一个实体类里面)。
tangyilong1994 2015-11-01
  • 打赏
  • 举报
回复
为什么不写个student类呢,在里面加上id userName passWord 等字段,从数据库读数据出来的时候构造一个student类,然后list.add(student);

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧