不废话,贴代码,记笔记,都懂。
当然,权限不能少: <uses-permission android:name="android.permission.READ_CONTACTS"/>
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;
}
}
}