• ContentProvider使用


     1 package activity.cyq.contentprovidertenderer;
     2 
     3 import android.content.ContentProvider;
     4 import android.content.ContentValues;
     5 import android.content.Context;
     6 import android.database.Cursor;
     7 import android.database.sqlite.SQLiteDatabase;
     8 import android.net.Uri;
     9 
    10 public class MyContentProvider extends ContentProvider {
    11     public static final Uri URI=Uri.parse("content://cyq.activity.MyContentProvider");
    12     private SQLiteDatabase database;
    13 
    14     @Override
    15     public boolean onCreate() {
    16         database = getContext().openOrCreateDatabase("ContentProvider", Context.MODE_PRIVATE, null);
    17         database.execSQL("Create Table contacts( _id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT DEFAULT NONE , cellPhotoNumber TEXT DEFAULT NONE) ");
    18         return false;
    19     }
    20 
    21     @Override
    22     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    23         Cursor cursor = database.query("contacts", null, null, null, null, null, null);
    24         return cursor;
    25     }
    26 
    27     @Override
    28     public String getType(Uri uri) {
    29         return null;
    30     }
    31 
    32     @Override
    33     public Uri insert(Uri uri, ContentValues values) {
    34         database.insert("contacts", "_id", values);
    35         return null;
    36     }
    37 
    38     @Override
    39     public int delete(Uri uri, String selection, String[] selectionArgs) {
    40         return 0;
    41     }
    42 
    43     @Override
    44     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    45         return 0;
    46     }
    47 }
    package activity.cyq.contentprovidertenderer;
    
    import android.content.ContentValues;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
        private EditText usernameEdit;
        private EditText cellPhotoNumberEdit;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            usernameEdit = (EditText) findViewById(R.id.username);
            cellPhotoNumberEdit = (EditText) findViewById(R.id.cellPhotoNumber);
    
            findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ContentValues values = new ContentValues();
                    values.put("username", usernameEdit.getText().toString());
                    values.put("cellPhotoNumber", cellPhotoNumberEdit.getText().toString());
                    getContentResolver().insert(MyContentProvider.URI, values);
                    Toast.makeText(MainActivity.this, "数据库创建成功", Toast.LENGTH_SHORT).show();
    
                }
            });
    
        }
    
    }
    
  • 相关阅读:
    文件加密二进制版本
    文件复制二进制版
    C语言里的文件函数
    文件加密2——基础版(限文本)
    文件加密——可执行,较复杂的抽取(限文本)
    typedef的使用3——使用经过typedef定义的函数构成的函数数组
    typedef的使用2——定义函数
    typedef的使用1——引入
    C语言枚举
    HanLP极致简繁转换详细讲解
  • 原文地址:https://www.cnblogs.com/chenyangqi/p/4712297.html
Copyright © 2020-2023  润新知