简体   繁体   English

如何使用SimpleCursorAdapter从数据库填充列表视图

[英]How to fill a listview from database using an SimpleCursorAdapter

I get some data from the database and try to fill them in a listview. 我从数据库中获取了一些数据,并尝试将其填充到列表视图中。 i have checked through cursor.count() there is data exist but the listview show anything :/ 我已经通过cursor.count()检查了是否存在数据,但listview显示了任何内容:/

I use a simpleCursorAdapter: 我使用一个simpleCursorAdapter:

    rubDb.open();   
    cur = rubDb.getRubParent();     
    startManagingCursor(cur);
    Log.d("cursor length",Integer.toString(cur.getCount()));
    String[] from = new String[] { RubriqueDbAdapter.RUB_NOM, RubriqueDbAdapter.RUB_VISUEL };       
    int[] to = new int[] {R.id.title, R.id.icon};
    listAdapter = new SimpleCursorAdapter(this,  R.layout.itemgauche, cur, from, to);
    listeGauche.setAdapter(listAdapter);


public Cursor getRubParent() {              
    Cursor cursorResults = mDb.rawQuery("SELECT * FROM " + TABLE_RUBRIQUE + " WHERE rub_id_parent = 0 ORDER BY rub_ordre ASC", null);       
    return cursorResults;
}

how can I get a picture from the sdcard and put it on the ImageView in the listview ? 我如何从sdcard获取图片并将其放在listview的ImageView中?

Thanks for your help ! 谢谢你的帮助 !

First, you should be using a CursorAdapter , perhaps a SimpleCursorAdapter , instead of manually copying all of the data into objects in an array, just to use ArrayAdapter . 首先,您应该使用CursorAdapter ,也许是SimpleCursorAdapter ,而不是仅使用ArrayAdapter ,而是将所有数据手动复制到数组中的对象中。

Second, if your list will be anything other than a simple piece of text, you must teach the adapter how to do whatever else you want. 第二,如果你的列表会比简单的一段文字其他任何东西 ,你必须适配器如何做你想要的任何东西。 With SimpleCursorAdapter , you could associate a ViewBinder with it, or subclass SimpleCursorAdapter and override setViewImage() , or subclass SimpleCursorAdapter and override bindView() . 使用SimpleCursorAdapter ,您可以将ViewBinder与之关联,或者将其子类SimpleCursorAdapter并重写setViewImage() ,或者将子类SimpleCursorAdapter并重写bindView()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM