-1

我想使用 cursorloader 同时获取所有联系人的 DISPLAY_NAME 和 COMAPNY 名称。而且我已经得到了它们的显示名称。我的问题是我不知道如何与 DISPLAY_NAME 同时获得 COMPANY 名称!你能告诉我解决我的问题的方法吗?这是我的代码。

public class MainActivity extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor>{
SimpleCursorAdapter mAdapter;       
LoaderManager loadermanager;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    loadermanager = getLoaderManager();     
    String[] fromColumns = {ContactsContract.Contacts._ID,
                            ContactsContract.Contacts.DISPLAY_NAME};        

    int[] toViews = {R.id.contactId,
                     R.id.contactName};

    mAdapter = new SimpleCursorAdapter(this,  
            R.layout.view_contact_entry, null,
            fromColumns, toViews, 0);


    setListAdapter(mAdapter);
    loadermanager.initLoader(1, null, this);
}

public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone._ID,
            ContactsContract.Contacts.DISPLAY_NAME};        
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";

    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";               
    return new CursorLoader(this, ContactsContract.Contacts.CONTENT_URI,
            projection, selection, null, sortOrder); 
}

public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    if(mAdapter!=null && cursor!=null)
        mAdapter.swapCursor(cursor);
    else
        Log.v("MAIN","OnLoadFinished: mAdapter is null");
}

public void onLoaderReset(Loader<Cursor> loader) {
    mAdapter.swapCursor(null);
}
@Override 
public void onListItemClick(ListView l, View v, int position, long id) {
    try {
        Cursor cs = (Cursor)l.getItemAtPosition(position);
        Intent  objIndent = new Intent(getApplicationContext(),DetailContact.class);
        objIndent.putExtra("contactId", cs.getString(0)); 
        objIndent.putExtra("contactName", cs.getString(1)); 
        startActivity(objIndent);   
    } catch (Exception e) {
        Log.e("ERROR: ", e.getMessage());
    }
}

}

4

1 回答 1

-1

我认为您需要查看针对ContactsContract.Data的查询

有一个从文档中的联系人加入和提取额外数据的示例

连同这个特殊的常量ContactsContract.CommonDataKinds.Organization.COMPANY

于 2013-08-14T03:22:28.747 回答