編輯:Android開發實例
首先,要有個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);
}
}
Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內置的媒體播放
在項目開發中,可能系統自帶的一些widget不能滿足我們的需求,這時就需要自定義View。 通過查看系統中的常用widget如Button,TextView,Ed
Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內置的媒體播放
從 Android 5.0 Lollipop 開始提供一套 API 來支持嵌入的滑動效果。同樣在最