这是我的代码,但每次我点击它强制关闭的联系人。是否有一个代码,以便当我得到联系人时,它将其添加到文本视图中?
public static final String TAG =“ContactManager”;
private Button mAddAccountButton;
private ListView mContactList;
private boolean mShowInvisible;
private CheckBox mShowInvisibleControl;
/**
* Called when the activity is first created. Responsible for initializing the UI.
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
Log.v(TAG, "Activity State: onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
// Obtain handles to UI objects
mAddAccountButton = (Button) findViewById(R.id.AddContact);
mContactList = (ListView) findViewById(R.id.ContactList);
mShowInvisibleControl = (CheckBox) findViewById(R.id.ShowInvisible);
// Initialize class properties
mShowInvisible = false;
mShowInvisibleControl.setChecked(mShowInvisible);
// Register handler for UI elements
mAddAccountButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "mAddAccountButton clicked");
launchContactAdder();
}
});
mShowInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG, "mShowInvisibleControl changed: " + isChecked);
mShowInvisible = isChecked;
populateContactList();
}
});
// Populate the contact list
populateContactList();
}
/**
* Populate the contact list based on account currently selected in the account spinner.
*/
private void populateContactList() {
// Build adapter with contact entries
Cursor cursor = getContacts();
String[] fields = new String[] {
ContactsContract.Data.DISPLAY_NAME
};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.main, cursor,
fields, new int[] {R.id.TextView01});
mContactList.setAdapter(adapter);
}
/**
* Obtains the contact list for the currently selected account.
*
* @return A cursor for for accessing the contact list.
*/
private Cursor getContacts()
{
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
(mShowInvisible ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
/**
* Launches the ContactAdder activity to add a new contact to the selected account.
*/
protected void launchContactAdder() {
Intent i = new Intent(this,Class1.class);
startActivity(i);
}
}
答案 0 :(得分:0)
根据我对联系人列表的经验,您需要根据可用内容设计查询。在1.6中,一个表的简单性包含所有信息。然而;随着2.0的曙光,他们推出了两张桌子。从一个表中获取ID并根据此ID查询以查找电话号码的位置。为了说明这一点,这里有一段对我有用的示例代码,虽然我遇到一些小问题,但有些联系人不会返回电话号码2/70,尽管所有70位用户都有ID和电话号码。我希望它有所帮助:
// look up contact via name
String name = contacts.getItem(arg1);
Uri lookup = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_FILTER_URI, name);
// look up id
Cursor c = getContentResolver().query(lookup, null, null, null, null);
String id = null;
int id_index = c.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
if (c.moveToFirst())
id = c.getString(id_index);
else
Toast.makeText(getApplicationContext(), "Friend not found",
Toast.LENGTH_SHORT).show();
c.close();
// use id if not null, to find contact's phone number / display name
if (id != null) {
String where = ContactsContract.Data.CONTACT_ID + " = " + id
+ " AND " + ContactsContract.Data.MIMETYPE + " = '"
+ ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
+ "'";
c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
null, where, null, null);
c.moveToFirst();
int iname = c
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
int iphone = c
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
if (c.getCount() > 0) {
_friend.setName(c.getString(iname));
_friend.setPhone(c.getString(iphone));
如果您有任何其他问题,请不要犹豫,我会尽力回答。对于我没有日志猫可以告诉你的是你正在尝试查询电话号码查询的正确表格结构。如果您尝试从返回0行的查询中访问信息,那么您将获得异常。请阅读该错误并显示它。
答案 1 :(得分:0)
您必须使用所有电子邮件,电话号码,网址等。
示例:
Linkify.addLinks(textView,Linkify.WEB_URLS);
了解更多详情: http://developer.android.com/reference/android/text/util/Linkify.html
注意:为此你不需要实现任何onClick等.Linkif自动管理它。