SimpleCursorAdapter ,SQLiteDatebase,ContentProvider 合用

注:由于转载时 代码乱了 ,所以没有加代码,不过有源代码。转:http://disanji.net/2011/04/03/android-use-simplecursoradapter/  这个地址代码代码排版可以。

如果使用Sqlite,建议和ContentProvider结合使用。这样数据库的生命周期就不用自己管了。然后,如果要在比如ListView中显示,可以使用CursorAdapter。简化的办法是使用子类SimpleCursorAdapter。

以下就介绍一下使用sqlite+content provider+cursor adapter的最简单实现示例。太简单了,示例如图:

首先,要有个Content provider,如不了解如何实现,请参考编写最简单的Content Provider

这里写的很简略,没用到的方法都没实现。

在总的布局中使用了ListView:

1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView android:id="@+id/riverList" android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

使用了自定义的ListView布局,见:

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content">
    <TextView android:id="@+id/riverName" android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

最后是在Activity中使用contentprovider查询的cursor,生成ListView:

  

httpeasymorse.googlecode.comsvntagsCustomListViewDemo-0.1

原文链接:http://marshal.easymorse.com/archives/4105



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值