Domanda

I am working on Contact base application in android platform, I want to write or save all the data of CONTACT phone app to my own Sqlite-database so i can add additional functionality,

Here is my source code:

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;

                    }
                }
            }
        }
È stato utile?

Soluzione

Class level:

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

onCreate: (Note clause exists for records with birthdays)

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

outside 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 is your Hashmap with records, write it to the DB.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top