问题描述
- 我想把SQLite数据库里的数据显示到ListView上,代码有什么问题吗?
-
private ListView listview = null;private List> list = new ArrayList>();
private SimpleAdapter simpleAdapter = null;
private ImageView iv;
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//适配器添加查询结果,并加到ListView中显示
simpleAdapter = new SimpleAdapter(this, getData(),
R.layout.item,new String[] {"name", "waimao","time","contact" ,"image"},
new int[] { R.id.name, R.id.waimao,R.id.time,R.id.contact,R.id.imageView1 });listview.setAdapter(simpleAdapter);
}
/**
*
* @return
*/private List> getData() {
DBHelper dbHelper = new DBHelper(this);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from post", null);
while (cursor.moveToNext()) {
Integer pid = cursor.getInt(cursor.getColumnIndex("id"));
String pname = cursor.getString(cursor.getColumnIndex("name"));
String waimao = cursor.getString(cursor.getColumnIndex("waimao"));
String time=cursor.getString(cursor.getColumnIndex("time"));
String contact=cursor.getString(cursor.getColumnIndex("contact"));
//从数据库中提取图片
byte[] pimage=null;
pimage=cursor.getBlob(cursor.getColumnIndex("image"));
Bitmap imagebitmap=BitmapFactory.decodeByteArray(pimage, 0, pimage.length);
iv=(ImageView)findViewById(R.id.imageView1);
iv.setImageBitmap(imagebitmap);
//这里同样是查询,只不过把查询到的数据添加到list集合,可以参照ListView的用法Map map = new HashMap();
map.put("id", pid); //获取_id
map.put("name", pname); //获取name
map.put("waimao", waimao);
map.put("time", time);
map.put("contact", contact);
map.put("image",imagebitmap);
list.add(map);} return list; }
解决方案
把 iv=..... 和 iv.set.... 两行删去
解决方案二:
先调用cursor.moveToFirst()
解决方案三:
已经解决了,但还是感谢你们