SimpleCursorAdapter的简单使用

SimpleCursorAdapter的简单实用


跟数据库交互式andorid程序最常用的功能,数据库和视图的同步,CursorAdapter是与数据库交互Adapter的最常用的类。同时它的子类SimpleCursorAdapter也是我们常用的操作类库。

这是一个用起来很方便的适配器类,它主要将Cursor与TexiView或ImageView进行映射。比如,你想设定要展示三列,那么当做好绑定之后,视图就会展示你设定好的那些列;当然了,视图的外观是定义在XML文件里面的,你只需用这个类与视图做好绑定就可以了。与视图绑定有两个阶段。

第一阶段:如果SimpleCursorAdapter.ViewBinder可用,将会调用setViewValue(android.view.View, android.database.Cursor, int)方法。该方法返回true就说明绑定成功,否则返回false ,

这就到了第二阶段,SimpleCursorAdapter内部开始自行绑定,过程是这样的,若绑定到TextView上,调用setViewText();若绑定到ImageView上,调用setViewImage();如果视图不是TextView或ImageView则抛出IllegalStateException异常。当使用带有过滤器的适配器时,例如,在APIDemo中有个AutoCompleteTextView的例子,我们能使用SimpleCursorAdapter.CursorToStringConverter和接口 FilterQueryProvider来控制过滤过程。

  1. SimpleCursorAdapter的声明。

public SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to, int flags)
标准构造器

Parameters 构造参数:

  • context The context where the ListView associated with this SimpleListItemFactory is running 应用程序上下文
  • layout resource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in “to” 布局资源文件的ID
  • c The database cursor. Can be null if the cursor is not available yet. 数据库的cursor
  • from A list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet. 数据库的列名
  • to The views that should display column in the “from” parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter. Can be null if the cursor is not available yet. 对应数据库的列应该对应的展示View的id
  • flags Flags used to determine the behavior of the adapter, as per CursorAdapter(Context, Cursor, int). 标志
// Create an empty adapter we will use to display the loaded data.
    mAdapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor,new String[] { KrystalContentProvider.COLUMN_IMAGEURL,KrystalContentProvider.COLUMN_NAME }, new int[] {R.id.item_image, R.id.item_text }, 0);

2 .SimpleCursorAdapter的简单使用

这里写图片描述

默认情况下,调用bindView (View view, Context context, Cursor cursor),默认情况下分为两步。如上所言,首先会看看SimpleCursorAdapter.ViewBinder是否可用,这是用户需要自己实现的内部类,具有较高的优先级,里面用户可以实现对于视图和数据库信息的绑定,如果setViewValue(android.view.View, android.database.Cursor, int)方法返回true,则绑定成功;如果返回false,则转到第二步。
第二步主要是对于ImageView和TextView的默认实现方法的调用。

setViewImage(ImageView, String)
setViewText(TextView, String) 

Binds all of the field names passed into the “to” parameter of the constructor with their corresponding cursor columns as specified in the “from” parameter. Binding occurs in two phases. First, if a SimpleCursorAdapter.ViewBinder is available, setViewValue(android.view.View, android.database.Cursor, int) is invoked. If the returned value is true, binding has occured.

If the returned value is false and the view to bind is a TextView, setViewText(TextView, String) is invoked. If the returned value is false and the view to bind is an ImageView, setViewImage(ImageView, String) is invoked. If no appropriate binding can be found, an IllegalStateException is thrown.

3.具体的代码实现

listView = (ListView) findViewById(R.id.listView1);
Cursor cursor = getContentResolver().query(
                KrystalContentProvider.Content_URI, null, "type=?",new String[] { "hot" }, null);
// Create an empty adapter we will use to display the loaded data.
        mAdapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor,
                new String[] { KrystalContentProvider.COLUMN_IMAGEURL,
                        KrystalContentProvider.COLUMN_NAME }, new int[] {
                        R.id.item_image, R.id.item_text }, 0);
        // 创建一个ViewBinder,处理视图和数据库数据的绑定
        SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() {

            @Override
            public boolean setViewValue(View view, Cursor cursor,
                    int columnIndex) {
                // 此处cursor已经定位到目标行
                Log.i("MainActivity", "columnIndex------" + columnIndex);
                /*
                 * 自己的糟粕 LayoutInflater
                 * inflater=LayoutInflater.from(MainActivity.this); View
                 * rootView=inflater.inflate(R.layout.list_item, null);
                 * if(columnIndex==1){ Log.i("MainActivity", "设置title");
                 * view=rootView.findViewById(R.id.item_text);
                 * 
                 * }else if(columnIndex==3){ Log.i("MainActivity", "设置图片");
                 * view=rootView.findViewById(R.id.item_image);
                 * //imageView.setImageURI
                 * (Uri.parse("file:///android_asset/krystal012.jpeg")); String
                 * uri=cursor.getString(columnIndex); Log.i("MainActivity",
                 * "设置图片----"+uri);
                 * //((ImageView)view).setImageURI(Uri.parse(cursor
                 * .getString(columnIndex)));
                 * Picasso.with(MainActivity.this).load
                 * ("file:///android_asset/krystal012.jpeg"
                 * ).into(((ImageView)view)); }
                 * 
                 * return true;
                 */
                // 如果目标视图类型为ImageView
                if (view instanceof ImageView) {
                    ImageView imageView = (ImageView) view;
                    String value = cursor.getString(columnIndex);
                    Log.i("MainActivity", "设置图片----" + value);
                    /*
                     * Uri uri=Uri.parse(value); imageView.setImageURI(uri);
                     */

                    Picasso.with(MainActivity.this).load(value).resize(90, 90)
                            .centerCrop().into(imageView);
                    return true;
                }
                // 默认返回false;
                return false;

            }
        };
        // 设置ViewBinder
        mAdapter.setViewBinder(binder);

        listView.setAdapter(mAdapter);

4.运行结果

可以实现动态加载类型为hot的krystal,实现简单的搜索和数据的显示。

这里写图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值