注:由于转载时 代码乱了 ,所以没有加代码,不过有源代码。转: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