自定义contentprovider
contentprovider 操作的数据源为本程序中创建的表中的数据
很简单的创建SqlHelper.java
//数据库创建
public class SqlHelper extends SQLiteOpenHelper{
public SqlHelper(Context context) {
//"data"数据库名字
super(context, "data", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql="create table test(_id INTEGER PRIMARY KEY AUTOINCREMENT,name text,age text)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
}
}
//自定义contentprovider
MyContentProvider.java
public class MyContentProvider extends ContentProvider{
// UriMatcher类用于匹配Uri
public final static UriMatcher uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
//域名,Contentprovider的标识
public final static String AUTHORIES="com.example.contentprovider.mydata";
static{
//添加要匹配的uri, 参数:域名、表名、返回匹配码(如果匹配正确则返回 1)
uriMatcher.addURI(AUTHORIES, "test", 1);
uriMatcher.addURI(AUTHORIES, "test/#", 2);//‘#’通配符号
}
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORIES+ "/" + "test");
private SqlHelper helper = new SqlHelper(getContext());
@Override
public int delete(Uri uri, String where, String[] whereArgs) {
SQLiteDatabase database = helper.getWritableDatabase();
//uri进行匹配,只有匹配才执行对应的操作
switch (uriMatcher.match(uri)) {
case 1:
int count=database.delete("test", where, whereArgs);//删除的记录的条数
break;
case 2:
break;
default:
break;
}
getContext().getContentResolver().notifyChange(uri, null);
return 0;
}
@Override
public String getType(Uri arg0) {
return null;
}
//插入操作
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
SQLiteDatabase database = helper.getWritableDatabase();
database.insert("test", "name", contentValues);//可以将“name”字段更改为其他字段都行
getContext().getContentResolver().notifyChange(uri, null);
return uri;
}
@Override
public boolean onCreate() {
helper = new SqlHelper(getContext());
return true;
}
@Override
public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,String arg4) {
return null;
}
@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
return 0;
}
}
//配置文件中注册如下
<!-- 注册ContentProvider -->
<provider
android:name="com.example.contentprovider.MyContentProvider"
android:authorities="com.example.contentprovider.mydata" />
//本程序的唯一一个javabean people.java
public class People {
private String age;
private String name;
private String id;
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
//MainActivity.java
public class MainActivity extends Activity {
public final static String AUTHORIES="com.example.contentprovider.mydata";
// | 固定格式 | |类似网站的域名| |数据源,这里是表|
final Uri uri = Uri.parse("content://" + AUTHORIES+ "/" + "test");
private Button button1;
private Button button2;
private ListView listView;
private List<People> peoples;
private DataAdapter dataAdapter;
@SuppressWarnings({ "unused", "unused" })
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)this.findViewById(R.id.button1);
button2=(Button)this.findViewById(R.id.button2);
listView = (ListView)this.findViewById(R.id.listview);
peoples = getData();
if (peoples == null) {
peoples = new ArrayList<People>();
}else {
dataAdapter = new DataAdapter(peoples);
}
listView.setAdapter(dataAdapter);
getContentResolver().registerContentObserver(uri, true, new DataObserver(new Handler()));//注册数据变化监听
//增加数据
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
ContentValues contentValues = new ContentValues();
contentValues.put("name", "hh");
contentValues.put("age", "20");
getContentResolver().insert(uri, contentValues);//插入数据
}
});
//删除数据
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Uri urif=ContentUris.withAppendedId(uri, 42);//
// getContentResolver().delete(uri, "_id=?", new String[]{" "+3});
getContentResolver().delete(urif, null, null);
}
});
}
private List<People> getData(){
List<People> list= new ArrayList<People>();
SqlHelper sqlHelper = new SqlHelper(MainActivity.this);
SQLiteDatabase database = sqlHelper.getWritableDatabase();
Cursor cursor = database.rawQuery("select * from test",null);
while (cursor.moveToNext()) {
People people = new People();
people.setId(cursor.getString(cursor.getColumnIndex("_id")));
people.setName(cursor.getString(cursor.getColumnIndex("name")));
people.setAge(cursor.getString(cursor.getColumnIndex("age")));
list.add(people);
}
return list;
}
//监听URI上的变化的类
public class DataObserver extends ContentObserver{
public DataObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean change) {
//进行相应的处理
List<People> getList = getData();
DataAdapter dataAdapter = new DataAdapter(getList);
listView.setAdapter(dataAdapter);
listView.setSelection(dataAdapter.getCount()-1);
}
}
public class DataAdapter extends BaseAdapter{
List<People> list = new ArrayList<People>();
public DataAdapter(List<People> list){
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int arg0) {
return list.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View view, ViewGroup arg2) {
Viewholder viewholder=null;
if (view == null) {
viewholder=new Viewholder();
view=LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
viewholder.idTextView = (TextView)view.findViewById(R.id.text);
viewholder.nameTextView = (TextView)view.findViewById(R.id.name);
viewholder.ageTextView = (TextView)view.findViewById(R.id.age);
view.setTag(viewholder);
}else {
viewholder=(Viewholder)view.getTag();
}
People people = list.get(arg0);
viewholder.idTextView.setText( people.getId());
viewholder.nameTextView.setText(people.getName());
viewholder.ageTextView.setText(people.getAge());
return view;
}
}
class Viewholder{
TextView idTextView;
TextView nameTextView;
TextView ageTextView;
}
}
//效果图