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);
    
        }

  • 相关阅读:
    android 入门-ID
    Win10 VS2015 社区版切换到VS2013社区版 进行维护之前的项目
    Win10 AppBar
    Win10 保存Element到相册
    LRUCache c#
    Winform解决界面重绘闪烁的问题
    使用Emit实现给实体赋值
    Winform 自定义窗体皮肤组件
    WPF 分享一种背景动画效果
    使用MEF与Castle实现AOP
  • 原文地址:https://www.cnblogs.com/android-deli/p/10122960.html
Copyright © 2011-2022 走看看