【问题标题】:How do I Save phone Contacts to my own database [closed]如何将电话联系人保存到我自己的数据库中[关闭]
【发布时间】:2014-02-04 06:14:24
【问题描述】:

我正在 android 平台上开发 Contact 基础应用程序,我想将 CONTACT 手机应用程序的所有数据写入或保存到我自己的 Sqlite 数据库中,以便我可以添加其他功能,

这是我的源代码:

package com.webpreneur_contactlist;



    import android.app.ListActivity;
    import android.content.Intent;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.support.v4.widget.SimpleCursorAdapter;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageButton;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.Toast;

        public class WebpreneurActivity extends ListActivity {

            private static final int CONTACT_CREATE = 0;
            private static final int CONTACT_EDIT = 1;
         public static  long id1;
            //select the second one, Android view menu
            private static final int INSERT_ID = Menu.FIRST;
            private static final int DELETE_ID = Menu.FIRST + 1;

            private DBHandler dbHelper;
            private Cursor c;
            ImageButton imageButton;
             public static long  rowId;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                Log.d("database1" ,"0");
                Log.d("Your Location4", "ok4:");
                super.onCreate(savedInstanceState);
                Log.d("database1" ,"1");
                setContentView(R.layout.activity_webpreneur);
                Log.d("database1" ,"2");
                dbHelper = new DBHandler(this);
                Log.d("database1" ,"3");
                dbHelper.open();
                fillData();
                //dbHelper.open();

                //addListenerOnButton();
            //dbHelper.addContact();

            imageButton = (ImageButton) findViewById(R.id.imageButton1);
            Log.d("database1" ,"button");
            imageButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Log.d("database1" ,"b4");
                    Intent i = new Intent(getApplicationContext(), ContactEdit.class);
                    startActivityForResult(i, CONTACT_CREATE);

                    Log.d("database1" ,"button3");
                    //fillData();

                }

            });

            //dbHelper.close();
        }








        @SuppressWarnings("deprecation")
        public void fillData() {
            Log.d("Your Location4", "ok6:");
            c = dbHelper.fetchAllRows();
            Log.d("Your Location4", "ok8:");
            //startManagingCursor(c);
            Log.d("Your Location4", "ok2:");
             NoIdCursorWrapper nc = new NoIdCursorWrapper(c, DBHandler.Key_ID);
            ListAdapter adapter = new SimpleCursorAdapter(this,
                    R.layout.contact_row, nc, new String[] { DBHandler.Key_Name,
                            DBHandler.Key_Phone }, new int[] { R.id.name,
                            R.id.phonenumber });
            setListAdapter(adapter);
        }




            protected void onListItemClick(ListView l, View v, int position,   long id) {
                super.onListItemClick(l, v, position, id);

                Intent i = new Intent(this, ContactEdit.class);
                i.putExtra(DBHandler.Key_ID, c.getLong(c.getColumnIndex(DBHandler.Key_ID)));
                i.putExtra(DBHandler.Key_Name, c.getString(c.getColumnIndex(DBHandler.Key_Name)));
                i.putExtra(DBHandler.Key_Address, c.getString(c.getColumnIndex(DBHandler.Key_Address)));
                i.putExtra(DBHandler.Key_Phone, c.getString(c.getColumnIndex(DBHandler.Key_Phone)));
                i.putExtra(DBHandler.Key_Website, c.getString(c.getColumnIndex(DBHandler.Key_Home)));
                startActivityForResult(i, CONTACT_EDIT);
        }
            protected void onActivityResult(int requestCode, int resultCode,
                    Intent data) {
                super.onActivityResult(requestCode, resultCode, data);

                if (resultCode == RESULT_OK) {
                    String name = data.getStringExtra(DBHandler.Key_Name);
                    String address = data.getStringExtra(DBHandler.Key_Address);
                    String mobile = data.getStringExtra(DBHandler.Key_Phone);
                    String home = data.getStringExtra(DBHandler.Key_Home);
                    //String id = data.getStringExtra(DBHandler.Key_ID);
                    switch (requestCode) {
                    case CONTACT_CREATE:
                        Log.d("Your Location4", "jj:");
                        dbHelper.createRow(name, address, mobile, home);
                    Log.d("Your Location4"  , "ok90:");
                        fillData();
                        break;
                    case CONTACT_EDIT:

                        //String id = data.getStringExtra(DBHandler.Key_ID);
                        //long rowId=Long.parseLong(DBHandler.Key_ID); ////Giving null pointer exception at this point
                        long rowId=data.getLongExtra(DBHandler.Key_ID, 0);
                         Log.d("Your Location4" , "ok9b:");
                        if (rowId != 0){
                            dbHelper.updateRow(rowId, name, address, mobile, home);
                        }
                        fillData();
                        break;

                    }
                }
            }
        }

【问题讨论】:

    标签: android sqlite android-contentprovider android-contacts


    【解决方案1】:

    班级等级:

    Uri uri;
    String[] projection;
    String where;
    String[] selectionArgs;
    

    onCreate: (Note 子句存在于有生日的记录)

    uri = ContactsContract.Data.CONTENT_URI; // Set the URI for Query of Contacts database
    
        //==================================================================
        //Projection String Array (Fields we are interested in capturing) 
        //==================================================================
        projection = new String[] { ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.Event.CONTACT_ID,
                ContactsContract.CommonDataKinds.Event.START_DATE,
                ContactsContract.CommonDataKinds.Email.DATA,
                ContactsContract.Contacts.HAS_PHONE_NUMBER,
                ContactsContract.CommonDataKinds.Phone.NUMBER
        };
    
    
        //==================================================================
        //Where Clause for the Database Query
        //==================================================================
        where = ContactsContract.Data.MIMETYPE + "= ? AND "
                + ContactsContract.CommonDataKinds.Event.TYPE + "="
                + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
    
    
    
    
        // Easy? Selection args
        selectionArgs = new String[] { ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE };
    
    
    
    getLoaderManager().initLoader(0, null, this); //Loader init
    

    onCreate 之外:

    @Override
        public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
    
            CursorLoader loader = new CursorLoader(this, uri, projection, where,
                    selectionArgs, null);
            return loader;
        }
    
        //  @SuppressWarnings("unchecked")
        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
            Map<String, List<String>> BD = new HashMap<String, List<String>>();
    
            if(cursor !=null){
                while (cursor.moveToNext()) {
    
                    List<String> li = new ArrayList<String>();
    
    
                    contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.CONTACT_ID));
                    li.add(contactId);
    
    
                    String displayBirthday = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
                    //==================================================================
                    // Handle the curious case of Date without Year
                    //==================================================================
                    if(displayBirthday.length()<8){
                        displayBirthday = displayBirthday.replace("--", "0001-"); 
                    }
                    //==================================================================
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    li.add(displayBirthday);
    
    
                    Uri phUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI,String.valueOf(contactId));
    
    
    
                    String Ur = String.valueOf(phUri);
    
    
                    li.add(Ur);
                    String DateStr = displayBirthday;
                    Date d = null;
                    try {
                        d = new SimpleDateFormat("yyyy-MM-dd"/*, current*/).parse(DateStr);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    java.sql.Date d1 = new java.sql.Date(d.getTime());
    
                    BD.put(name, li);
                    li = null;
    
                }
    

    BD 是带有记录的 Hashmap,将其写入数据库。

    【讨论】:

    • 但问题是我如何将这些细节添加到我自己的数据库“DBHANDLER”中,
    • 从 HashMap 中获取单个元素并写入?还要记住 Maps 有一个唯一的键,重写意味着丢失以前的键数据。您可以跳过添加到地图中,而是在获得 em 后立即插入值!
    • 对不起修女,我是初学者,所以我不认识你,请帮帮我
    • 检查 onLoadFinished 函数,在 while 循环中你会得到所有值,现在你需要将它们相应地添加到你的数据库中
    • 如果我想获取联系人的照片、gmail 联系人以及电话联系人的照片怎么办?
    猜你喜欢
    • 2017-12-01
    • 1970-01-01
    • 2023-04-05
    • 2014-11-17
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多