阳春面 | 关注互联网,Android

三/10

27

如何处理SimpleCursorAdapter不能自动转换的数据

SimpleCursorAdapter可以把我们从Array或数据库中取出的数据绑定的ListView或其他的组件,这个很好用,但有时候有些View,SimpleCursorAdapter并不能直接绑定,需要自己去实现它的setViewBinder方法,下面是我利用SimpleCursorAdapter绑定RatingBar的例子:

SimpleCursorAdapter timeItems = new SimpleCursorAdapter(this,
                R.layout.timeitem_row, timeItemsCursor, from, to);
        timeItems.setViewBinder(new SimpleCursorAdapter.ViewBinder(){

            @Override
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                int rateIndex = cursor.getColumnIndex("rate");
                if(columnIndex == rateIndex)
                {
                    RatingBar ratingBar = (RatingBar)view;
                    ratingBar.setRating(cursor.getInt(rateIndex));
                    return true;
                }
                return false;
            }
        });

· ·

No comments yet.

Leave a Reply

<<

>>