教材:武汉科技大学 吴志祥老师 的andriod 教程 SQLite数据库编程 和ContentProvider
Sqlite 简单CRUD参考教材即可
效果
MainActivity 中的添加功能
case R.id.id_add_img:
final View addDialog=getLayoutInflater().inflate(R.layout.add_contact,null);
new AlertDialog.Builder(MainActivity.this).setView(addDialog).setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
EditText mname=(EditText)addDialog.findViewById(R.id.name);
EditText mphone=(EditText)addDialog.findViewById(R.id.phone);
final String name=mname.getText().toString();
final String phone=mphone.getText().toString();
// 创建一个空的ContentValues
ContentValues values = new ContentValues();
// 向RawContacts.CONTENT_URI执行一个空值插入
// 目的是获取系统返回的rawContactId
Uri rawContactUri = getContentResolver().insert(
ContactsContract.RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
// 设置内容类型
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
// 设置联系人名字
values.put(StructuredName.GIVEN_NAME, name);
// 向联系人URI添加联系人名字
getContentResolver().insert(ContactsContract
.Data.CONTENT_URI, values);
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
// 设置联系人的电话号码
values.put(Phone.NUMBER, phone);
// 设置电话类型
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
// 向联系人电话号码URI添加电话号码
getContentResolver().insert(ContactsContract
.Data.CONTENT_URI, values);
values.clear();
selectFragment(2);
Toast.makeText(MainActivity.this, "联系人数据添加成功",
Toast.LENGTH_SHORT).show();
}
}).show();
break;
Fragment 中展示
public class contactFragment extends Fragment {
private RecyclerView recyclerView;
//private List<String> mList = new ArrayList<>();
private List<Map<String,String>>data=new ArrayList<Map<String, String>>();
private HashMap<String, String> item;
private Context context;
private adapter_expand adapter;
public contactFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.tab03, container, false);
recyclerView=view.findViewById(R.id.rcv_expandcollapse);
context=this.getActivity();
initexpandData();
adapter=new adapter_expand(context,data);
LinearLayoutManager manager=new LinearLayoutManager(context);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);
recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
return view;
}
private void initexpandData(){
Uri uri= ContactsContract.Contacts.CONTENT_URI;
ContentResolver resolver = context.getContentResolver();
Cursor cursor=resolver.query(uri, null, null, null, null); //得到记录集
if(cursor!=null){
while(cursor.moveToNext()){
//先获取联系人_id字段的索引号后再获取_id值
int idFieldIndex=cursor.getColumnIndex("_id");
int id=cursor.getInt(idFieldIndex);
//先获取联系人姓名字段的索引号后再获取姓名字段值
int nameFieldIndex = cursor.getColumnIndex("display_name");
String name=cursor.getString(nameFieldIndex);
int numCountFieldIndex=cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
int numCount=cursor.getInt(numCountFieldIndex); //获取联系人的电话号码个数
String phoneNumber="";
if(numCount>0){ //联系人有至少一个电话号码
//在类ContactsContract.CommonDataKinds.Phone中根据id查询相应联系人的所有电话;
Cursor phonecursor=getActivity().getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?",
new String[]{Integer.toString(id)}, null);
if(phonecursor!=null){
if(phonecursor.moveToFirst()){ //仅读取第一个电话号码
int numFieldIndex=phonecursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
phoneNumber=phonecursor.getString(numFieldIndex);
}
phonecursor.close();
}
}
item=new HashMap<String,String>(); //必须循环创建
item.put("name", name);
item.put("phoneNumber", phoneNumber);
data.add(item);
}
cursor.close();
}
}
}
效果:
项目源码1 sqlite
项目源码2 contentprovider