andriod学习 作业四 Sqlite和contentprovider

教材:武汉科技大学 吴志祥老师 的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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值