Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android 淺談SimpleCursorAdapter

Android 淺談SimpleCursorAdapter

編輯:Android開發實例


       如果使用Sqlite,建議和ContentProvider結合使用。這樣數據庫的生命周期就不用自己管了。然後,如果要在比如ListView中顯示,可以使用CursorAdapter。簡化的辦法是使用子類SimpleCursorAdapter。

       以下就介紹一下使用sqlite+content provider+cursor adapter的最簡單實現示例。太簡單了,示例如圖:


        首先,要有個Content provider,如不了解如何實現,請參考編寫最簡單的Content Provider和在Content provider實現中使用SQLiteOpenHelper,下面寫的是結合二者的:


java代碼:

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:

java代碼:
<?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布局,見:

java代碼:
<?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:

java代碼:
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);
}
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved