使用 startsWith 和 replaceFirst 对我不起作用。
using startsWith and replaceFirst not working for me.
我使用以下代码在 Android 监视器中打印我所有的 phone 联系人。 phone 数字以 00 开头,我想将 00 更改为 + 。但它不起作用。你能告诉我有什么问题吗?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getInt(cur.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
if (phoneNo.startsWith("00")) {
System.out.println(phoneNo.replaceFirst("00", "+"));
}
System.out.println("Name: " + name);
System.out.println("Phone No: " + phoneNo);
}
pCur.close();
}
}
}
replaceFirst()
returns a String
,它不会改变对象。您应该执行作业:
phoneNo = phoneNo.replaceFirst("00", "+")
我使用以下代码在 Android 监视器中打印我所有的 phone 联系人。 phone 数字以 00 开头,我想将 00 更改为 + 。但它不起作用。你能告诉我有什么问题吗?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getInt(cur.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
if (phoneNo.startsWith("00")) {
System.out.println(phoneNo.replaceFirst("00", "+"));
}
System.out.println("Name: " + name);
System.out.println("Phone No: " + phoneNo);
}
pCur.close();
}
}
}
replaceFirst()
returns a String
,它不会改变对象。您应该执行作业:
phoneNo = phoneNo.replaceFirst("00", "+")