zoukankan      html  css  js  c++  java
  • Android-ListView-SimpleCursorAdapter

    在上篇博客,Android-ListView-SimpleAdapter,中介绍了SimpleAdapter的使用操作(SimpleAdapter面向的数据是非Cursor数据),而SimpleCursorAdapter面向的数据是(Cursor数据)

    SimpleCursorAdapter 也是 BaseAdapter的子类,SimpleCursorAdapter内部实现是继承了BaseAdapter,对Cursor进行了逻辑包装等操作

    布局代码:

    <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/ll_et_id"
            android:layout_marginTop="20dp"></ListView>

    Java代码:

       /**
         * 查询
         */
        public void query(View view) {
            cursor = contentResolver.query(uri,
                    new String[]{"_id", "name", "age"},
                    null, null
                    , null, null);
    
            /**
             * 使用SimpleCursorAdapter 适配器
             */
            SimpleCursorAdapter simpleCursorAdapter = new
                    SimpleCursorAdapter(MainActivity.this, // 上下文
                    R.layout.layout_item, // Item布局
                    cursor, // Cursor 查询出来的游标 这里面有数据库里面的数据
                    new String[]{"_id", "name", "age"}, // 从哪里来,指的是 查询出数据库列名
                    new int[]{R.id.tv_id, R.id.tv_name, R.id.tv_age}, // 到哪里去,指的是,把查询出来的数据,赋值给Item布局 的控件
                    SimpleCursorAdapter.NO_SELECTION);
    
            // 设置ListView适配器
            listview.setAdapter(simpleCursorAdapter);
    
        }

  • 相关阅读:
    【原】AMFObject数据格式详解
    STL总结 (C++)
    git相关项目迁移
    OBS_Classic经典版框架
    windows线程同步的几种方式
    面试题之strcpy / strlen / strcat / strcmp的实现
    python实用技巧
    Python Flask学习笔记之数据库
    Python Flask学习笔记之Web表单
    必应每日壁纸批量下载
  • 原文地址:https://www.cnblogs.com/android-deli/p/10122960.html
Copyright © 2011-2022 走看看