操作界面:
public class MainActivity extends Activity {
private static final String URISTRING_NOTES = "content://com.zdsoft.notesProvider/notes";
private ListView listview_note;
private Button bt_add;
private Button bt_search;
private EditText et_id;
private EditText et_title;
private EditText et_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
loadListView();
bt_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
ContentResolver contentResolver = getContentResolver();
Uri insertUri = Uri.parse(URISTRING_NOTES);
ContentValues values = new ContentValues();
values.put("title", et_title.getText().toString());
values.put("content", et_content.getText().toString());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
values.put("times", df.format(new Date()));
Uri uri = contentResolver.insert(insertUri, values);
Toast.makeText(MainActivity.this, "添加成功,新id为:"+ContentUris.parseId(uri), Toast.LENGTH_LONG).show();
loadListView();
clearEditTextValue();
}
});
bt_search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Cursor cursor=getDataItem();
if(cursor!=null){
if(cursor.moveToFirst()){
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String content = cursor.getString(cursor.getColumnIndex("content"));
et_id.setText(String.valueOf(id));
et_title.setText(title);
et_content.setText(content);
}
}
}
});
}
private void init(){
listview_note=(ListView)findViewById(R.id.listview_note);
bt_add=(Button)findViewById(R.id.bt_add);
bt_search=(Button)findViewById(R.id.bt_search);
et_id=(EditText)findViewById(R.id.et_id);
et_title=(EditText)findViewById(R.id.et_title);
et_content=(EditText)findViewById(R.id.et_content);
}
/**
* 清空值
*/
private void clearEditTextValue(){
et_id.setText("");
et_title.setText("");
et_content.setText("");
}
/**
* 加载ListView
*/
@SuppressWarnings("deprecation")
private void loadListView(){
SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this,R.layout.listview_item,getDataList(),
new String[]{"_id","title","content"},
new int[]{R.id.tv_id,R.id.tv_title,R.id.tv_content});
listview_note.setAdapter(adapter);
}
/**
* 读取外部系统Cursor数据
* @return
*/
private Cursor getDataList() {
//从数据库读取listview数据
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.parse("content://com.zdsoft.notesProvider/notes");
//用getTpye来获取返回结果是什么类型的东西
String type=contentResolver.getType(uri);
Toast.makeText(MainActivity.this, "返回类型为:"+type, Toast.LENGTH_SHORT).show();
Cursor cursor=contentResolver.query(uri, null, null, null, null);
return cursor;
}
/**
* 读取外部系统Cursor数据
* @return
*/
private Cursor getDataItem() {
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.parse(URISTRING_NOTES);
if(et_id.getText().toString()==null||"".equals(et_id.getText().toString())){
Toast.makeText(MainActivity.this, "请输入id查询", Toast.LENGTH_SHORT).show();
return null;
}else{
Uri selectUri=Uri.withAppendedPath(uri, et_id.getText().toString());
//Uri selectUri=Uri.withAppendedPath(baseUri, pathSegment)
//用getTpye来获取返回结果是什么类型的东西
String type=contentResolver.getType(selectUri);
Toast.makeText(MainActivity.this, "返回类型为:"+type, Toast.LENGTH_SHORT).show();
System.out.println(selectUri);
Cursor cursor=contentResolver.query(selectUri, null, null, null, null);
return cursor;
}
}
}
被操作应用:
public class privide extends ContentProvider {
private SQLiteDatabase sql;
static UriMatcher match = new UriMatcher(UriMatcher.NO_MATCH);
static {
match.addURI("utils.privide1", "message", 1);
match.addURI("utils.privide1", "message/#", 2);
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
sql = new dbhelp(getContext()).getReadableDatabase();
return false;
}
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
// TODO Auto-generated method stub
switch (match.match(arg0)) {
case 1:
// 此为没有条件的删除
int l = sql.delete("message", arg1, arg2);
return l;
case 2:
// 此为有条件的删除,因没有条件的删除的条件属性为null,所以需要判断是否为null
Long id = ContentUris.parseId(arg0);
String selection = "id=" + id;
if (arg1 != null && !arg1.equals("")) {
arg1 = selection + "and" + arg1;
int le = sql.delete("message", arg1, arg2);
return le;
}
default:
return 0;
}
}
@Override
public String getType(Uri arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri arg0, ContentValues arg1) {
// TODO Auto-generated method stub
switch (match.match(arg0)) {
case 1:
Long id = sql.insert("message", "title", arg1);
Uri u1 = ContentUris.withAppendedId(arg0, id);
return u1;
case 2:
Long id2 = sql.insert("message", null, arg1);
Uri u2 = ContentUris.withAppendedId(arg0, id2);
return u2;
default:
return null;
}
}
@Override
public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3, String arg4) {
// TODO Auto-generated method stub
switch (match.match(arg0)) {
case 1:
Cursor c1 = sql.query("message", arg1, arg2, arg3, null, null, null);
return c1;
case 2:
Long id = ContentUris.parseId(arg0);
String where = "id" + id;
if (arg2 != null && !arg2.equals("")) {
arg2 = arg2 + "and" + where;
// sql.query(table, columns, selection, selectionArgs, groupBy,
// having, orderBy)
Cursor c2 = sql.query("message", arg1, arg2, arg3, null, null, null);
return c2;
}
default:
return null;
}
}
@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
// TODO Auto-generated method stub
switch (match.match(arg0)) {
case 1:
int i1 = sql.update("message", arg1, arg2, arg3);
return i1;
case 2:
Long id = ContentUris.parseId(arg0);
String where = "id" + id;
if (arg2 != null && !arg2.equals("")) {
arg2 = arg2 + "and" + where;
int i2 = sql.update("message", arg1, arg2, arg3);
return i2;
}
default:
return 0;
}
}
}