Search code examples
androidandroid-spinner

Storing retrieved contacts phone in to database as a string


I am fetching the list of contacts from phone. I am trying to store the selected contact to the database since I want to retrieve the same later. I am trying to store it as a string. I am using spinner to display list of contacts. But the database is storing the contacts as shown below:

android.content.ContentResolver$CursorWrapperInner@1516bf58

But the application while displaying displays the contacts properly as shown below:

9983635353 Keshav

I want to store the similar way as I see. I have written the following code for this, but presently I am not able to achieve that. pleas let me know what changes do I need to make in order to get this working. All suggestions are welcome. Thanks in advance.

My code to fetch the contact and save them:

contacts1 = (Spinner) findViewById(R.id.spinnerContacts);

 cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

        String[] from = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone._ID};
        int[] to = {R.id.textViewNumber, R.id.textViewEmail};


        myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.custom_spinner_row, cursor1, from, to, 0);

contacts1.setAdapter(myCursorAdapter);
contacts1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
               contacts = parent.getItemAtPosition(position).toString();
                Log.d("Pana", "The value of the contact field is " +parent.getItemAtPosition(position).toString());
                Toast.makeText(getApplicationContext(), "The value of Position selected is " +position, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
long id = sqliteHelper.insertData( contacts );

Below is my insertData method of database:

public long insertData( String contacts) {

        db = helper.getWritableDatabase();

        ContentValues contentValues = new ContentValues();


        contentValues.put(SQLiteHelper.CONTACTS, contacts);




        id = db.insert(SQLiteHelper.TABLE_NAME, null, contentValues);


        Log.d("PaNa", "The value of Id is " + id);

        db.close();
        return id;
    }

Please let me know what changes do I need to make to save the contact data exactly as I see in the Display of application. Thanks in advance.


Solution

  • Here:

    parent.getItemAtPosition(position)
    

    Will return Cursor object for selected row instead of String.

    Cursor selectedCursor=(Cursor)parent.getItemAtPosition(position);
    String strNumber=selectedCursor.getString(0);
    String strName=selectedCursor.getString(1);