編輯:Android開發實例
在Android開發中,如果用到了Sqlite,那麼最好和ContentProvider結合使用。這樣數據庫的生命周期就不用自己操心了。然後,如果要在比如ListView中顯示,可以使用CursorAdapter。簡化的辦法是使用子類SimpleCursorAdapter。以下就介紹一下使用sqlite+content provider+cursor adapter的最簡單實現示例。示例如圖:
首先,要有個Content provider,如不了解如何實現,請參考編寫最簡單的Content Provider和在Content provider實現中使用SQLiteOpenHelper,下面寫的是結合二者的:
public class RiverContentProvider extends ContentProvider {
public static final Uri CONTENT_URI = Uri
.parse("content://com.easymorse.cp.rivers");
public static final String _ID = "_id";
public static final String NAME = "name";
public static final String LENGTH = "length";
private static SQLiteDatabase database;
private static final int DATABASE_VERSION = 2;
private static final List<River> RIVERS = new ArrayList<River>();
static {
River river = new River("長江", 6380);
RIVERS.add(river);
river = new River("黃河", 5464);
RIVERS.add(river);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onCreate() {
database = new RiverDatabaseHelper(getContext(), "rivers", null,
DATABASE_VERSION).getReadableDatabase();
return database != null;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
return database.query("rivers", projection, selection, selectionArgs,
null, null, sortOrder);
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
private static class RiverDatabaseHelper extends SQLiteOpenHelper {
public RiverDatabaseHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL("create table if not exists rivers("
+ " _id integer primary key autoincrement," + " name text,"
+ "length integer" + ");");
SQLiteStatement statement = database
.compileStatement("insert into rivers(name,length) values(?,?)");
for (River r : RIVERS) {
int index = 1;
statement.bindString(index++, r.getName());
statement.bindLong(index++, r.getLength());
statement.executeInsert();
}
statement.close();
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion,
int newVersion) {
database.execSQL("drop table if exists rivers");
onCreate(database);
}
}
這裡寫的很簡略,沒用到的方法都沒實現。
在總的布局中使用了ListView:
<?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布局,見:
<?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:
public class ListViewActivity extends Activity {
private ListView riverListView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRiverListViewAdapter();
}
private void setRiverListViewAdapter() {
riverListView = (ListView) this.findViewById(R.id.riverList);
Cursor cursor = managedQuery(RiverContentProvider.CONTENT_URI, null,
null, null, null);
CursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row,
cursor, new String[] { RiverContentProvider.NAME },
new int[] { R.id.riverName });
riverListView.setAdapter(adapter);
}
}
AppWidgetProvider 用來在HOME頁面顯示插件 實現步驟:1、為AppWidget提供一個元布局文件AppWigdetProvider_Provi
Android平台包括JET引擎,可以在應用程序中添加JET音頻內容的交互播放。 Android提供JetPlayer類來處理這些東西。為了使用Jet內容,需要使用Jet
本文實例講述了Android編程之菜單實現方法。分享給大家供大家參考,具體如下: 菜單是許多應用程序不可或缺的一部分,Android中更是如此,所有搭載Andro
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩