安卓四大元件之一ContentProvider內容提供者
阿新 • • 發佈:2019-02-16
前提:這裡我們需要準備好資料庫,及資料的建立。可以使用Naviact,當然也可以用安卓程式碼。這裡我們用安卓程式碼來實現。
1.建立一個專案,名為contentprovider . 在xml檔案裡寫兩個輸入框,一個按鈕,用來新增資料。如下圖:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 然後寫一個Dbhelp類用來建立資料庫。程式碼如下: public class DBhelp extends SQLiteOpenHelper{ ----繼承一個SQLiteOpenHelper public DbHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
Log.i("test","構造方法");
}
@Override
public void onCreate(SQLiteDatabase db) {
//建立表
// Log.i("test","onCreate");
db.execSQL("create table tbUser(_id integer primary key autoincrement,uname,uage)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { ---資料庫版本升級後呼叫(修改資料庫後呼叫)
Log.i("test","onUpgrade");
} } 就這樣DBHELP類寫完了。 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 接著我們就開始寫MainActivity
public classMainActivity extends AppCompatActivity { private EditText et_main_uname; private EditText et_main_uage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_main_uname = (EditText) findViewById(R.id.et_main_uname); et_main_uage = (EditText) findViewById(R.id.et_main_uage); } //操作按鈕的方法 public void operation(View view) { String uname = et_main_uname.getText().toString(); String uage = et_main_uage.getText().toString(); Dbhelp dbHelper = new Dbhelp(this, "G160828.db", null, 1); SQLiteDatabase sqLiteDatabase = dbHelper.getReadableDatabase();for (int i = 1; i <= 100; i++) { sqLiteDatabase.execSQL("insert into tbUser(uname,uage) values('" + uname + i + "'," + uage + ")"); } Toast.makeText(this, "操作成功", Toast.LENGTH_SHORT).show(); } }
讓後我們就可以開始寫提供者了 建立一個class檔案 public class MyProvider extends ContentProvider {
private SQLiteDatabase sqLiteDatabase;
@Override
public boolean onCreate() {
DbHelper dbHelper=new DbHelper(getContext(),"G160828.db",null,1);
sqLiteDatabase = dbHelper.getReadableDatabase();
return false;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
//查詢資料
Log.i("test","查詢");
return sqLiteDatabase.rawQuery("select * from tbUser",null);
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
return null;
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
return 0;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
return 0;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------- 然後到清單檔案裡配置一下:注意要到<activity>標籤外邊 <!--配置 內容提供者-->
<provider
android:authorities="com.zking.administrator.g160828_android14_sqlite.User"
android:exported="true"
android:name=".MyProvider"></provider>
提供者就寫完了!!!! ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
接下來我們寫訪問者,訪問提供者裡面的資料。 簡單的在xml裡寫個按鈕,就像這樣:
然後mainActivity檔案裡就這樣寫: 之後執行一下,去控制檯看有沒有資料,如果有就代表成功了! 就像這樣:
好了,到這裡就全部講完了!