将检索到的联系人 phone 作为字符串存储到数据库中
Storing retrieved contacts phone in to database as a string
我正在从 phone 获取联系人列表。我试图将选定的联系人存储到数据库中,因为我想稍后检索它。我试图将它存储为一个字符串。我正在使用微调器来显示联系人列表。但是数据库正在存储联系人,如下所示:
android.content.ContentResolver$CursorWrapperInner@1516bf58
但应用程序在显示时正确显示联系人,如下所示:
9983635353
凯沙夫
我想按照我看到的方式存储。我为此编写了以下代码,但目前我无法实现。请让我知道我需要进行哪些更改才能使其正常工作。欢迎所有建议。提前致谢。
我获取联系人并保存它们的代码:
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 );
下面是我的数据库insertData方法:
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;
}
请告诉我需要进行哪些更改才能完全按照我在应用程序显示中看到的那样保存联系人数据。提前致谢。
这里:
parent.getItemAtPosition(position)
将为所选行 return Cursor
对象而不是字符串。
Cursor selectedCursor=(Cursor)parent.getItemAtPosition(position);
String strNumber=selectedCursor.getString(0);
String strName=selectedCursor.getString(1);
我正在从 phone 获取联系人列表。我试图将选定的联系人存储到数据库中,因为我想稍后检索它。我试图将它存储为一个字符串。我正在使用微调器来显示联系人列表。但是数据库正在存储联系人,如下所示:
android.content.ContentResolver$CursorWrapperInner@1516bf58
但应用程序在显示时正确显示联系人,如下所示:
9983635353 凯沙夫
我想按照我看到的方式存储。我为此编写了以下代码,但目前我无法实现。请让我知道我需要进行哪些更改才能使其正常工作。欢迎所有建议。提前致谢。
我获取联系人并保存它们的代码:
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 );
下面是我的数据库insertData方法:
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;
}
请告诉我需要进行哪些更改才能完全按照我在应用程序显示中看到的那样保存联系人数据。提前致谢。
这里:
parent.getItemAtPosition(position)
将为所选行 return Cursor
对象而不是字符串。
Cursor selectedCursor=(Cursor)parent.getItemAtPosition(position);
String strNumber=selectedCursor.getString(0);
String strName=selectedCursor.getString(1);